public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 00/14] Add --enable-newlib-reent-thread-local option
@ 2022-06-21 12:49 Sebastian Huber
  2022-06-21 12:49 ` [PATCH 01/14] Move content in <sys/reent.h> Sebastian Huber
                   ` (14 more replies)
  0 siblings, 15 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

By default, Newlib uses a huge object of type struct _reent to store
thread-specific data.  This object is returned by __getreent() if the
__DYNAMIC_REENT__ Newlib configuration option is defined.

The reentrancy structure contains for example errno and the standard input,
output, and error file streams.  This means that if an application only uses
errno it has a dependency on the file stream support even if it does not use
it.  This is an issue for lower end targets and applications which need to
qualify the software according to safety standards (for example ECSS-E-ST-40C,
ECSS-Q-ST-80C, IEC 61508, ISO 26262, DO-178, DO-330, DO-333).

If the new _REENT_THREAD_LOCAL configuration option is enabled, then struct
_reent is replaced by dedicated thread-local objects for each struct _reent
member.  The thread-local objects are defined in translation units which use
the corresponding object.

Patches 1 to 13 contain no functional changes (hopefully).  They just introduce
macros to be able to use struct _reent members or thread-local objects
depending on the new _REENT_THREAD_LOCAL option.  Patch 14 adds the new
configuration option.

Matt Joyce (13):
  Define _REENT_EMERGENCY(ptr) only once
  Add _REENT_ERRNO(ptr)
  Add _REENT_STDIN(ptr)
  Add _REENT_STDOUT(ptr)
  Add _REENT_STDERR(ptr)
  Add _REENT_INC(ptr)
  Add _REENT_LOCALE(ptr)
  Add _REENT_CLEANUP(ptr)
  Add _REENT_CVTLEN(ptr)
  Add _REENT_CVTBUF(ptr)
  Add _REENT_SIG_FUNC(ptr)
  Add _REENT_IS_NULL()
  Add --enable-newlib-reent-thread-local option

Sebastian Huber (1):
  Move content in <sys/reent.h>

 newlib/README                             |   8 ++
 newlib/configure                          |  21 ++++
 newlib/configure.ac                       |  15 +++
 newlib/libc/ctype/wctrans.c               |   2 +-
 newlib/libc/ctype/wctype.c                |   2 +-
 newlib/libc/errno/errno.c                 |   8 +-
 newlib/libc/include/stdio.h               |  12 +-
 newlib/libc/include/sys/config.h          |   6 +
 newlib/libc/include/sys/errno.h           |   8 +-
 newlib/libc/include/sys/reent.h           | 132 +++++++++++++++++++---
 newlib/libc/include/wchar.h               |   8 +-
 newlib/libc/locale/locale.c               |   4 +
 newlib/libc/locale/setlocale.h            |   4 +-
 newlib/libc/locale/uselocale.c            |   4 +-
 newlib/libc/machine/cris/sys/errno.h      |   2 +-
 newlib/libc/machine/powerpc/strtosfix16.c |  10 +-
 newlib/libc/machine/powerpc/strtosfix32.c |  10 +-
 newlib/libc/machine/powerpc/strtosfix64.c |  10 +-
 newlib/libc/machine/powerpc/strtoufix16.c |  10 +-
 newlib/libc/machine/powerpc/strtoufix32.c |  10 +-
 newlib/libc/machine/powerpc/strtoufix64.c |  10 +-
 newlib/libc/machine/spu/c99ppe.h          |   8 +-
 newlib/libc/machine/spu/stdio.c           |  16 +--
 newlib/libc/machine/spu/sys/errno.h       |   2 +-
 newlib/libc/reent/closer.c                |   2 +-
 newlib/libc/reent/execr.c                 |   6 +-
 newlib/libc/reent/fcntlr.c                |   2 +-
 newlib/libc/reent/fstat64r.c              |   2 +-
 newlib/libc/reent/fstatr.c                |   2 +-
 newlib/libc/reent/gettimeofdayr.c         |   2 +-
 newlib/libc/reent/impure.c                |   4 +
 newlib/libc/reent/isattyr.c               |   2 +-
 newlib/libc/reent/linkr.c                 |   2 +-
 newlib/libc/reent/lseek64r.c              |   2 +-
 newlib/libc/reent/lseekr.c                |   2 +-
 newlib/libc/reent/mkdirr.c                |   2 +-
 newlib/libc/reent/open64r.c               |   2 +-
 newlib/libc/reent/openr.c                 |   2 +-
 newlib/libc/reent/readr.c                 |   2 +-
 newlib/libc/reent/reent.c                 |  18 +--
 newlib/libc/reent/renamer.c               |   2 +-
 newlib/libc/reent/sbrkr.c                 |   2 +-
 newlib/libc/reent/signalr.c               |   2 +-
 newlib/libc/reent/stat64r.c               |   2 +-
 newlib/libc/reent/statr.c                 |   2 +-
 newlib/libc/reent/unlinkr.c               |   2 +-
 newlib/libc/reent/writer.c                |   2 +-
 newlib/libc/signal/signal.c               |  36 +++---
 newlib/libc/stdio/asniprintf.c            |   4 +-
 newlib/libc/stdio/asnprintf.c             |   4 +-
 newlib/libc/stdio/fdopen.c                |   2 +-
 newlib/libc/stdio/fflush.c                |  18 +--
 newlib/libc/stdio/fileno.c                |   2 +-
 newlib/libc/stdio/fileno_u.c              |   2 +-
 newlib/libc/stdio/findfp.c                |  25 ++--
 newlib/libc/stdio/flags.c                 |   2 +-
 newlib/libc/stdio/fmemopen.c              |  14 +--
 newlib/libc/stdio/fopencookie.c           |  14 +--
 newlib/libc/stdio/fpurge.c                |   2 +-
 newlib/libc/stdio/freopen.c               |   4 +-
 newlib/libc/stdio/fseeko.c                |   4 +-
 newlib/libc/stdio/ftell.c                 |   2 +-
 newlib/libc/stdio/ftello.c                |   2 +-
 newlib/libc/stdio/funopen.c               |  16 +--
 newlib/libc/stdio/fvwrite.c               |   4 +-
 newlib/libc/stdio/local.h                 |   3 +-
 newlib/libc/stdio/mktemp.c                |  14 +--
 newlib/libc/stdio/nano-vfprintf.c         |  10 +-
 newlib/libc/stdio/open_memstream.c        |  14 +--
 newlib/libc/stdio/perror.c                |   2 +-
 newlib/libc/stdio/refill.c                |   2 +-
 newlib/libc/stdio/setvbuf.c               |   2 +-
 newlib/libc/stdio/sniprintf.c             |   8 +-
 newlib/libc/stdio/snprintf.c              |   8 +-
 newlib/libc/stdio/swprintf.c              |   8 +-
 newlib/libc/stdio/tmpfile.c               |   6 +-
 newlib/libc/stdio/tmpnam.c                |  13 ++-
 newlib/libc/stdio/vasniprintf.c           |   2 +-
 newlib/libc/stdio/vasnprintf.c            |   2 +-
 newlib/libc/stdio/vfprintf.c              |  12 +-
 newlib/libc/stdio/vfscanf.c               |   2 +-
 newlib/libc/stdio/vfwprintf.c             |   4 +-
 newlib/libc/stdio/vfwscanf.c              |   2 +-
 newlib/libc/stdio/vsniprintf.c            |   4 +-
 newlib/libc/stdio/vsnprintf.c             |   4 +-
 newlib/libc/stdio/vswprintf.c             |   4 +-
 newlib/libc/stdio/wsetup.c                |   2 +-
 newlib/libc/stdio64/fdopen64.c            |   2 +-
 newlib/libc/stdio64/freopen64.c           |   4 +-
 newlib/libc/stdio64/fseeko64.c            |   6 +-
 newlib/libc/stdio64/ftello64.c            |   2 +-
 newlib/libc/stdio64/tmpfile64.c           |   6 +-
 newlib/libc/stdlib/__adjust.c             |   4 +-
 newlib/libc/stdlib/dtoa.c                 |   5 +
 newlib/libc/stdlib/ecvtbuf.c              |  25 ++--
 newlib/libc/stdlib/l64a.c                 |   4 +
 newlib/libc/stdlib/lcong48.c              |   8 ++
 newlib/libc/stdlib/mblen.c                |   4 +
 newlib/libc/stdlib/mbrlen.c               |   4 +
 newlib/libc/stdlib/mbrtowc.c              |   8 +-
 newlib/libc/stdlib/mbsnrtowcs.c           |   6 +-
 newlib/libc/stdlib/mbtowc.c               |   4 +
 newlib/libc/stdlib/mbtowc_r.c             |  34 +++---
 newlib/libc/stdlib/mprec.c                |   5 +
 newlib/libc/stdlib/rand.c                 |   4 +
 newlib/libc/stdlib/strtod.c               |  10 +-
 newlib/libc/stdlib/strtoimax.c            |   4 +-
 newlib/libc/stdlib/strtol.c               |   2 +-
 newlib/libc/stdlib/strtoll.c              |   2 +-
 newlib/libc/stdlib/strtoul.c              |   2 +-
 newlib/libc/stdlib/strtoull.c             |   2 +-
 newlib/libc/stdlib/strtoumax.c            |   4 +-
 newlib/libc/stdlib/wcrtomb.c              |   8 +-
 newlib/libc/stdlib/wcsnrtombs.c           |   6 +-
 newlib/libc/stdlib/wcstod.c               |   4 +-
 newlib/libc/stdlib/wcstoimax.c            |   4 +-
 newlib/libc/stdlib/wcstol.c               |   2 +-
 newlib/libc/stdlib/wcstoll.c              |   2 +-
 newlib/libc/stdlib/wcstoul.c              |   2 +-
 newlib/libc/stdlib/wcstoull.c             |   4 +-
 newlib/libc/stdlib/wcstoumax.c            |   4 +-
 newlib/libc/stdlib/wctomb.c               |   4 +
 newlib/libc/stdlib/wctomb_r.c             |  18 +--
 newlib/libc/string/strerror.c             |   2 +-
 newlib/libc/string/strsignal.c            |   4 +
 newlib/libc/string/strtok.c               |   4 +
 newlib/libc/sys/arm/syscalls.c            |   3 +-
 newlib/libc/time/asctime.c                |   4 +
 newlib/libc/time/gmtime.c                 |   4 +
 newlib/libm/math/w_lgamma.c               |   4 +
 newlib/newlib.hin                         |   4 +
 winsup/cygwin/cygerrno.h                  |   4 +-
 winsup/cygwin/cygtls.cc                   |   2 +-
 winsup/cygwin/dcrt0.cc                    |   2 +-
 winsup/cygwin/errno.cc                    |   4 +-
 winsup/cygwin/strfuncs.cc                 |  12 +-
 winsup/cygwin/syscalls.cc                 |   4 +-
 137 files changed, 619 insertions(+), 332 deletions(-)

-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 01/14] Move content in <sys/reent.h>
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 12:49 ` [PATCH 02/14] Define _REENT_EMERGENCY(ptr) only once Sebastian Huber
                   ` (13 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

Move definitions not directly related to struct _reent to the bottom of the
file.  This allows a contiguous #ifndef _REENT_THREAD_LOCAL_STORAGE block.
---
 newlib/libc/include/sys/reent.h | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 2d144f653..e9bded6c7 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -725,11 +725,6 @@ struct _reent
     _REENT_INIT_PTR_ZEROED(var); \
   }
 
-/* This value is used in stdlib/misc.c.  reent/reent.c has to know it
-   as well to make sure the freelist is correctly free'd.  Therefore
-   we define it here, rather than in stdlib/misc.c, as before. */
-#define _Kmax (sizeof (size_t) << 3)
-
 /*
  * All references to struct _reent are via this pointer.
  * Internally, newlib routines that need to reference it should use _REENT.
@@ -747,13 +742,6 @@ extern struct _reent *_impure_ptr __ATTRIBUTE_IMPURE_PTR__;
 
 extern struct _reent _impure_data __ATTRIBUTE_IMPURE_DATA__;
 
-extern void (*__stdio_exit_handler) (void);
-
-void _reclaim_reent (struct _reent *);
-
-extern int _fwalk_sglue (struct _reent *, int (*)(struct _reent *, __FILE *),
-			 struct _glue *);
-
 /* #define _REENT_ONLY define this to get only reentrant routines */
 
 #if defined(__DYNAMIC_REENT__) && !defined(__SINGLE_THREAD__)
@@ -767,9 +755,21 @@ extern int _fwalk_sglue (struct _reent *, int (*)(struct _reent *, __FILE *),
 
 #define _GLOBAL_REENT (&_impure_data)
 
+/* This value is used in stdlib/misc.c.  reent/reent.c has to know it
+   as well to make sure the freelist is correctly free'd.  Therefore
+   we define it here, rather than in stdlib/misc.c, as before. */
+#define _Kmax (sizeof (size_t) << 3)
+
 extern struct _atexit *__atexit; /* points to head of LIFO stack */
 extern struct _atexit __atexit0; /* one guaranteed table, required by ANSI */
 
+extern void (*__stdio_exit_handler) (void);
+
+void _reclaim_reent (struct _reent *);
+
+extern int _fwalk_sglue (struct _reent *, int (*)(struct _reent *, __FILE *),
+			 struct _glue *);
+
 #ifdef __cplusplus
 }
 #endif
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 02/14] Define _REENT_EMERGENCY(ptr) only once
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
  2022-06-21 12:49 ` [PATCH 01/14] Move content in <sys/reent.h> Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 12:49 ` [PATCH 03/14] Add _REENT_ERRNO(ptr) Sebastian Huber
                   ` (12 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

Use this macro to access the _emergency member of struct _reent.  This macro
will help to replace the _emergency member of struct _reent with a thread-local
storage object in a follow up patch.
---
 newlib/libc/include/sys/reent.h | 4 ++--
 newlib/libc/reent/reent.c       | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index e9bded6c7..2b0ff46b7 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -531,7 +531,6 @@ struct _reent
 #define _REENT_MP_FREELIST(ptr)	((ptr)->_mp->_freelist)
 #define _REENT_ASCTIME_BUF(ptr)	((ptr)->_asctime_buf)
 #define _REENT_TM(ptr)		((ptr)->_localtime_buf)
-#define _REENT_EMERGENCY(ptr)	((ptr)->_emergency)
 #define _REENT_STRTOK_LAST(ptr)	((ptr)->_misc->_strtok_last)
 #define _REENT_MBLEN_STATE(ptr)	((ptr)->_misc->_mblen_state)
 #define _REENT_MBTOWC_STATE(ptr)((ptr)->_misc->_mbtowc_state)
@@ -704,7 +703,6 @@ struct _reent
 #define _REENT_MP_FREELIST(ptr)	((ptr)->_freelist)
 #define _REENT_ASCTIME_BUF(ptr)	((ptr)->_new._reent._asctime_buf)
 #define _REENT_TM(ptr)		(&(ptr)->_new._reent._localtime_buf)
-#define _REENT_EMERGENCY(ptr)	((ptr)->_emergency)
 #define _REENT_STRTOK_LAST(ptr)	((ptr)->_new._reent._strtok_last)
 #define _REENT_MBLEN_STATE(ptr)	((ptr)->_new._reent._mblen_state)
 #define _REENT_MBTOWC_STATE(ptr)((ptr)->_new._reent._mbtowc_state)
@@ -720,6 +718,8 @@ struct _reent
 
 #endif /* !_REENT_SMALL */
 
+#define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
+
 #define _REENT_INIT_PTR(var) \
   { memset((var), 0, sizeof(*(var))); \
     _REENT_INIT_PTR_ZEROED(var); \
diff --git a/newlib/libc/reent/reent.c b/newlib/libc/reent/reent.c
index d61415901..a9d44b455 100644
--- a/newlib/libc/reent/reent.c
+++ b/newlib/libc/reent/reent.c
@@ -62,8 +62,8 @@ _reclaim_reent (struct _reent *ptr)
 #endif
 
 #ifdef _REENT_SMALL
-      if (ptr->_emergency)
-	_free_r (ptr, ptr->_emergency);
+      if (_REENT_EMERGENCY(ptr))
+	_free_r (ptr, _REENT_EMERGENCY(ptr));
       if (ptr->_mp)
 	_free_r (ptr, ptr->_mp);
       if (ptr->_r48)
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 03/14] Add _REENT_ERRNO(ptr)
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
  2022-06-21 12:49 ` [PATCH 01/14] Move content in <sys/reent.h> Sebastian Huber
  2022-06-21 12:49 ` [PATCH 02/14] Define _REENT_EMERGENCY(ptr) only once Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
       [not found]   ` <BN2P110MB15447CD8C6DD815779AA7E4C9AB39@BN2P110MB1544.NAMP110.PROD.OUTLOOK.COM>
  2022-06-21 12:49 ` [PATCH 04/14] Add _REENT_STDIN(ptr) Sebastian Huber
                   ` (11 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

Add a _REENT_ERRNO() macro to encapsulate the access to the
_errno member of struct reent. This will help to replace the
structure member with a thread-local storage object in a follow
up patch.
---
 newlib/libc/ctype/wctrans.c               |  2 +-
 newlib/libc/ctype/wctype.c                |  2 +-
 newlib/libc/errno/errno.c                 |  2 +-
 newlib/libc/include/sys/errno.h           |  2 +-
 newlib/libc/include/sys/reent.h           |  1 +
 newlib/libc/machine/cris/sys/errno.h      |  2 +-
 newlib/libc/machine/powerpc/strtosfix16.c | 10 +++----
 newlib/libc/machine/powerpc/strtosfix32.c | 10 +++----
 newlib/libc/machine/powerpc/strtosfix64.c | 10 +++----
 newlib/libc/machine/powerpc/strtoufix16.c | 10 +++----
 newlib/libc/machine/powerpc/strtoufix32.c | 10 +++----
 newlib/libc/machine/powerpc/strtoufix64.c | 10 +++----
 newlib/libc/machine/spu/stdio.c           |  2 +-
 newlib/libc/machine/spu/sys/errno.h       |  2 +-
 newlib/libc/reent/closer.c                |  2 +-
 newlib/libc/reent/execr.c                 |  6 ++--
 newlib/libc/reent/fcntlr.c                |  2 +-
 newlib/libc/reent/fstat64r.c              |  2 +-
 newlib/libc/reent/fstatr.c                |  2 +-
 newlib/libc/reent/gettimeofdayr.c         |  2 +-
 newlib/libc/reent/isattyr.c               |  2 +-
 newlib/libc/reent/linkr.c                 |  2 +-
 newlib/libc/reent/lseek64r.c              |  2 +-
 newlib/libc/reent/lseekr.c                |  2 +-
 newlib/libc/reent/mkdirr.c                |  2 +-
 newlib/libc/reent/open64r.c               |  2 +-
 newlib/libc/reent/openr.c                 |  2 +-
 newlib/libc/reent/readr.c                 |  2 +-
 newlib/libc/reent/renamer.c               |  2 +-
 newlib/libc/reent/sbrkr.c                 |  2 +-
 newlib/libc/reent/signalr.c               |  2 +-
 newlib/libc/reent/stat64r.c               |  2 +-
 newlib/libc/reent/statr.c                 |  2 +-
 newlib/libc/reent/unlinkr.c               |  2 +-
 newlib/libc/reent/writer.c                |  2 +-
 newlib/libc/signal/signal.c               |  6 ++--
 newlib/libc/stdio/asniprintf.c            |  4 +--
 newlib/libc/stdio/asnprintf.c             |  4 +--
 newlib/libc/stdio/fdopen.c                |  2 +-
 newlib/libc/stdio/fflush.c                | 18 ++++++------
 newlib/libc/stdio/fileno.c                |  2 +-
 newlib/libc/stdio/fileno_u.c              |  2 +-
 newlib/libc/stdio/findfp.c                |  2 +-
 newlib/libc/stdio/flags.c                 |  2 +-
 newlib/libc/stdio/fmemopen.c              | 14 +++++-----
 newlib/libc/stdio/fopencookie.c           | 14 +++++-----
 newlib/libc/stdio/fpurge.c                |  2 +-
 newlib/libc/stdio/freopen.c               |  4 +--
 newlib/libc/stdio/fseeko.c                |  4 +--
 newlib/libc/stdio/ftell.c                 |  2 +-
 newlib/libc/stdio/ftello.c                |  2 +-
 newlib/libc/stdio/funopen.c               | 16 +++++------
 newlib/libc/stdio/fvwrite.c               |  4 +--
 newlib/libc/stdio/mktemp.c                | 14 +++++-----
 newlib/libc/stdio/nano-vfprintf.c         | 10 +++----
 newlib/libc/stdio/open_memstream.c        | 14 +++++-----
 newlib/libc/stdio/perror.c                |  2 +-
 newlib/libc/stdio/refill.c                |  2 +-
 newlib/libc/stdio/sniprintf.c             |  8 +++---
 newlib/libc/stdio/snprintf.c              |  8 +++---
 newlib/libc/stdio/swprintf.c              |  8 +++---
 newlib/libc/stdio/tmpfile.c               |  6 ++--
 newlib/libc/stdio/tmpnam.c                |  2 +-
 newlib/libc/stdio/vasniprintf.c           |  2 +-
 newlib/libc/stdio/vasnprintf.c            |  2 +-
 newlib/libc/stdio/vfprintf.c              | 12 ++++----
 newlib/libc/stdio/vfscanf.c               |  2 +-
 newlib/libc/stdio/vfwprintf.c             |  4 +--
 newlib/libc/stdio/vfwscanf.c              |  2 +-
 newlib/libc/stdio/vsniprintf.c            |  4 +--
 newlib/libc/stdio/vsnprintf.c             |  4 +--
 newlib/libc/stdio/vswprintf.c             |  4 +--
 newlib/libc/stdio/wsetup.c                |  2 +-
 newlib/libc/stdio64/fdopen64.c            |  2 +-
 newlib/libc/stdio64/freopen64.c           |  4 +--
 newlib/libc/stdio64/fseeko64.c            |  6 ++--
 newlib/libc/stdio64/ftello64.c            |  2 +-
 newlib/libc/stdio64/tmpfile64.c           |  6 ++--
 newlib/libc/stdlib/__adjust.c             |  4 +--
 newlib/libc/stdlib/mbrtowc.c              |  4 +--
 newlib/libc/stdlib/mbsnrtowcs.c           |  2 +-
 newlib/libc/stdlib/mbtowc_r.c             | 34 +++++++++++------------
 newlib/libc/stdlib/strtod.c               | 10 +++----
 newlib/libc/stdlib/strtoimax.c            |  4 +--
 newlib/libc/stdlib/strtol.c               |  2 +-
 newlib/libc/stdlib/strtoll.c              |  2 +-
 newlib/libc/stdlib/strtoul.c              |  2 +-
 newlib/libc/stdlib/strtoull.c             |  2 +-
 newlib/libc/stdlib/strtoumax.c            |  4 +--
 newlib/libc/stdlib/wcrtomb.c              |  4 +--
 newlib/libc/stdlib/wcsnrtombs.c           |  2 +-
 newlib/libc/stdlib/wcstod.c               |  4 +--
 newlib/libc/stdlib/wcstoimax.c            |  4 +--
 newlib/libc/stdlib/wcstol.c               |  2 +-
 newlib/libc/stdlib/wcstoll.c              |  2 +-
 newlib/libc/stdlib/wcstoul.c              |  2 +-
 newlib/libc/stdlib/wcstoull.c             |  4 +--
 newlib/libc/stdlib/wcstoumax.c            |  4 +--
 newlib/libc/stdlib/wctomb_r.c             | 18 ++++++------
 newlib/libc/string/strerror.c             |  2 +-
 winsup/cygwin/cygerrno.h                  |  4 +--
 winsup/cygwin/errno.cc                    |  4 +--
 winsup/cygwin/strfuncs.cc                 | 12 ++++----
 winsup/cygwin/syscalls.cc                 |  4 +--
 104 files changed, 250 insertions(+), 249 deletions(-)

diff --git a/newlib/libc/ctype/wctrans.c b/newlib/libc/ctype/wctrans.c
index 4a58df152..dbb103d38 100644
--- a/newlib/libc/ctype/wctrans.c
+++ b/newlib/libc/ctype/wctrans.c
@@ -84,7 +84,7 @@ _wctrans_r (struct _reent *r,
     return WCT_TOUPPER;
   else
     {
-      r->_errno = EINVAL;
+      _REENT_ERRNO(r) = EINVAL;
       return 0;
     }
 }
diff --git a/newlib/libc/ctype/wctype.c b/newlib/libc/ctype/wctype.c
index 5f2ae7a25..8c7afc731 100644
--- a/newlib/libc/ctype/wctype.c
+++ b/newlib/libc/ctype/wctype.c
@@ -128,7 +128,7 @@ _wctype_r (struct _reent *r,
     }
 
   /* otherwise invalid */
-  r->_errno = EINVAL;
+  _REENT_ERRNO(r) = EINVAL;
   return 0;
 }
 
diff --git a/newlib/libc/errno/errno.c b/newlib/libc/errno/errno.c
index fd1743d73..70402924b 100644
--- a/newlib/libc/errno/errno.c
+++ b/newlib/libc/errno/errno.c
@@ -10,7 +10,7 @@
 int *
 __errno ()
 {
-  return &_REENT->_errno;
+  return &_REENT_ERRNO(_REENT);
 }
 
 #endif
diff --git a/newlib/libc/include/sys/errno.h b/newlib/libc/include/sys/errno.h
index 025b461d4..995f30e05 100644
--- a/newlib/libc/include/sys/errno.h
+++ b/newlib/libc/include/sys/errno.h
@@ -26,7 +26,7 @@ extern __IMPORT char *program_invocation_name;
 extern __IMPORT char *program_invocation_short_name;
 #endif
 
-#define __errno_r(ptr) ((ptr)->_errno)
+#define __errno_r(ptr) _REENT_ERRNO(ptr)
 
 #define	EPERM 1		/* Not owner */
 #define	ENOENT 2	/* No such file or directory */
diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 2b0ff46b7..70a987100 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -719,6 +719,7 @@ struct _reent
 #endif /* !_REENT_SMALL */
 
 #define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
+#define _REENT_ERRNO(_ptr)	((_ptr)->_errno)
 
 #define _REENT_INIT_PTR(var) \
   { memset((var), 0, sizeof(*(var))); \
diff --git a/newlib/libc/machine/cris/sys/errno.h b/newlib/libc/machine/cris/sys/errno.h
index 3d573015d..c8113ed73 100644
--- a/newlib/libc/machine/cris/sys/errno.h
+++ b/newlib/libc/machine/cris/sys/errno.h
@@ -24,7 +24,7 @@ extern int *__errno (void);
 extern const char * const _sys_errlist[];
 extern int _sys_nerr;
 
-#define __errno_r(ptr) ((ptr)->_errno)
+#define __errno_r(ptr) _REENT_ERRNO(ptr)
 
 /* Adjusted to the linux asm/errno.h */
 #define	EPERM		 1	/* Operation not permitted */
diff --git a/newlib/libc/machine/powerpc/strtosfix16.c b/newlib/libc/machine/powerpc/strtosfix16.c
index 3fba45409..5475de88e 100644
--- a/newlib/libc/machine/powerpc/strtosfix16.c
+++ b/newlib/libc/machine/powerpc/strtosfix16.c
@@ -108,10 +108,10 @@ _strtosfix16_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return SHRT_MIN;
       return SHRT_MAX;
@@ -120,12 +120,12 @@ _strtosfix16_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return SHRT_MAX;
     }
   else if (dbl.d < -1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return SHRT_MIN;
     }
 
@@ -152,7 +152,7 @@ _strtosfix16_r (struct _reent *rptr,
       /* check if positive saturation has occurred because of rounding */
       if (!sign && result < 0)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return SHRT_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtosfix32.c b/newlib/libc/machine/powerpc/strtosfix32.c
index b21de9ea1..6941be5d5 100644
--- a/newlib/libc/machine/powerpc/strtosfix32.c
+++ b/newlib/libc/machine/powerpc/strtosfix32.c
@@ -29,10 +29,10 @@ _strtosfix32_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return LONG_MIN;
       return LONG_MAX;
@@ -41,12 +41,12 @@ _strtosfix32_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return LONG_MAX;
     }
   else if (dbl.d < -1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return LONG_MIN;
     }
 
@@ -75,7 +75,7 @@ _strtosfix32_r (struct _reent *rptr,
       /* check if positive saturation has occurred because of rounding */
       if (!sign && result < 0)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return LONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtosfix64.c b/newlib/libc/machine/powerpc/strtosfix64.c
index 16032128f..9da3ffaee 100644
--- a/newlib/libc/machine/powerpc/strtosfix64.c
+++ b/newlib/libc/machine/powerpc/strtosfix64.c
@@ -32,10 +32,10 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (ld_type == 1)
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(ldbl) & Sign_bit)
 	return LONG_LONG_MIN;
       return LONG_LONG_MAX;
@@ -63,7 +63,7 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (exp > 0 || (exp == 0 && tmp != 0x8000000000000000LL))
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return LONG_LONG_MIN;
 	}
     }
@@ -71,7 +71,7 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (exp >= 0)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return LONG_LONG_MAX;
 	}
     }
@@ -88,7 +88,7 @@ _strtosfix64_r (struct _reent *rptr,
       /* check if positive saturation has occurred because of rounding */
       if (!sign && result < 0)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return LONG_LONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtoufix16.c b/newlib/libc/machine/powerpc/strtoufix16.c
index 9b16268bb..415652dd2 100644
--- a/newlib/libc/machine/powerpc/strtoufix16.c
+++ b/newlib/libc/machine/powerpc/strtoufix16.c
@@ -106,10 +106,10 @@ _strtoufix16_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return 0;
       return USHRT_MAX;
@@ -118,12 +118,12 @@ _strtoufix16_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return USHRT_MAX;
     }
   else if (dbl.d < 0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return 0;
     }
 
@@ -147,7 +147,7 @@ _strtoufix16_r (struct _reent *rptr,
       if (negexp == 0)
 	{
 	  /* we have overflow which means saturation */
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return USHRT_MAX;
 	}
       result |= (1 << (16 - negexp));
diff --git a/newlib/libc/machine/powerpc/strtoufix32.c b/newlib/libc/machine/powerpc/strtoufix32.c
index 7d03f8c9b..4340c711f 100644
--- a/newlib/libc/machine/powerpc/strtoufix32.c
+++ b/newlib/libc/machine/powerpc/strtoufix32.c
@@ -28,10 +28,10 @@ _strtoufix32_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return 0;
       return ULONG_MAX;
@@ -40,12 +40,12 @@ _strtoufix32_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return ULONG_MAX;
     }
   else if (dbl.d < 0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return 0;
     }
 
@@ -77,7 +77,7 @@ _strtoufix32_r (struct _reent *rptr,
       /* if rounding causes carry, then saturation has occurred */
       if (result < tmp)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return ULONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtoufix64.c b/newlib/libc/machine/powerpc/strtoufix64.c
index a2f0484b3..205706856 100644
--- a/newlib/libc/machine/powerpc/strtoufix64.c
+++ b/newlib/libc/machine/powerpc/strtoufix64.c
@@ -31,10 +31,10 @@ _strtoufix64_r (struct _reent *rptr,
     {
       if (ld_type == 1)
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(ldbl) & Sign_bit)
 	return 0;
       return ULONG_LONG_MAX;
@@ -60,14 +60,14 @@ _strtoufix64_r (struct _reent *rptr,
   /* check for saturation */
   if (sign)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return 0;
     }
   else
     {
       if (exp > 0 || (exp == 0 && tmp >= 0x8000000000000000LL))
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return ULONG_LONG_MAX;
 	}
     }
@@ -89,7 +89,7 @@ _strtoufix64_r (struct _reent *rptr,
 	  /* if rounding causes carry, then saturation has occurred */
 	  if (result < tmp)
 	    {
-	      rptr->_errno = ERANGE;
+	      _REENT_ERRNO(rptr) = ERANGE;
 	      return ULONG_LONG_MAX;
 	    }
 	}
diff --git a/newlib/libc/machine/spu/stdio.c b/newlib/libc/machine/spu/stdio.c
index 87e4c406d..1dcc88b60 100644
--- a/newlib/libc/machine/spu/stdio.c
+++ b/newlib/libc/machine/spu/stdio.c
@@ -47,7 +47,7 @@ __sfp (struct _reent *d)
       return &__fp[i];
     }
   }
-  d->_errno = EMFILE;
+  _REENT_ERRNO(d) = EMFILE;
   return NULL;
 }
 
diff --git a/newlib/libc/machine/spu/sys/errno.h b/newlib/libc/machine/spu/sys/errno.h
index b008ff6f4..8758283eb 100644
--- a/newlib/libc/machine/spu/sys/errno.h
+++ b/newlib/libc/machine/spu/sys/errno.h
@@ -34,7 +34,7 @@ extern "C" {
 extern const char * const _sys_errlist[];
 extern int _sys_nerr;
 
-#define __errno_r(ptr) ((ptr)->_errno)
+#define __errno_r(ptr) _REENT_ERRNO(ptr)
 
 /* Adjusted to the linux asm/errno.h */
 #define	EPERM		 1	/* Operation not permitted */
diff --git a/newlib/libc/reent/closer.c b/newlib/libc/reent/closer.c
index deb34b002..2d72b2ab5 100644
--- a/newlib/libc/reent/closer.c
+++ b/newlib/libc/reent/closer.c
@@ -45,7 +45,7 @@ _close_r (ptr, fd)
 
   errno = 0;
   if ((ret = _close (fd)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/execr.c b/newlib/libc/reent/execr.c
index 59b61223e..541fb8624 100644
--- a/newlib/libc/reent/execr.c
+++ b/newlib/libc/reent/execr.c
@@ -54,7 +54,7 @@ _execve_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _execve (name, argv, env)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
@@ -86,7 +86,7 @@ _fork_r (struct _reent *ptr)
 
   errno = 0;
   if ((ret = _fork ()) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
@@ -118,7 +118,7 @@ _wait_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _wait (status)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fcntlr.c b/newlib/libc/reent/fcntlr.c
index cd19d226f..f60aa091c 100644
--- a/newlib/libc/reent/fcntlr.c
+++ b/newlib/libc/reent/fcntlr.c
@@ -49,7 +49,7 @@ _fcntl_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _fcntl (fd, cmd, arg)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fstat64r.c b/newlib/libc/reent/fstat64r.c
index c546f5c1d..a049206e2 100644
--- a/newlib/libc/reent/fstat64r.c
+++ b/newlib/libc/reent/fstat64r.c
@@ -55,7 +55,7 @@ _fstat64_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _fstat64 (fd, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fstatr.c b/newlib/libc/reent/fstatr.c
index ec906c98d..9a02e9a68 100644
--- a/newlib/libc/reent/fstatr.c
+++ b/newlib/libc/reent/fstatr.c
@@ -53,7 +53,7 @@ _fstat_r (ptr, fd, pstat)
 
   errno = 0;
   if ((ret = _fstat (fd, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/gettimeofdayr.c b/newlib/libc/reent/gettimeofdayr.c
index 9b982a993..aa60e5e3a 100644
--- a/newlib/libc/reent/gettimeofdayr.c
+++ b/newlib/libc/reent/gettimeofdayr.c
@@ -60,7 +60,7 @@ _gettimeofday_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _gettimeofday (ptimeval, ptimezone)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/isattyr.c b/newlib/libc/reent/isattyr.c
index f21bf25b2..f76945519 100644
--- a/newlib/libc/reent/isattyr.c
+++ b/newlib/libc/reent/isattyr.c
@@ -50,7 +50,7 @@ _isatty_r (ptr, fd)
 
   errno = 0;
   if ((ret = _isatty (fd)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/linkr.c b/newlib/libc/reent/linkr.c
index b22da5f94..169b6eeec 100644
--- a/newlib/libc/reent/linkr.c
+++ b/newlib/libc/reent/linkr.c
@@ -51,7 +51,7 @@ _link_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _link (old, new)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/lseek64r.c b/newlib/libc/reent/lseek64r.c
index 40769fb6d..c9afd01df 100644
--- a/newlib/libc/reent/lseek64r.c
+++ b/newlib/libc/reent/lseek64r.c
@@ -50,7 +50,7 @@ _lseek64_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _lseek64 (fd, pos, whence)) == (_off64_t) -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/lseekr.c b/newlib/libc/reent/lseekr.c
index ac2daaab9..77e66b8e1 100644
--- a/newlib/libc/reent/lseekr.c
+++ b/newlib/libc/reent/lseekr.c
@@ -47,7 +47,7 @@ _lseek_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _lseek (fd, pos, whence)) == (_off_t) -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/mkdirr.c b/newlib/libc/reent/mkdirr.c
index fd72df64c..cf66a24d3 100644
--- a/newlib/libc/reent/mkdirr.c
+++ b/newlib/libc/reent/mkdirr.c
@@ -48,7 +48,7 @@ _mkdir_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _mkdir (path, mode)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/open64r.c b/newlib/libc/reent/open64r.c
index 84bd67e34..fe288d083 100644
--- a/newlib/libc/reent/open64r.c
+++ b/newlib/libc/reent/open64r.c
@@ -52,7 +52,7 @@ _open64_r (ptr, file, flags, mode)
 
   errno = 0;
   if ((ret = _open64 (file, flags, mode)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/openr.c b/newlib/libc/reent/openr.c
index c6a7db5de..824315438 100644
--- a/newlib/libc/reent/openr.c
+++ b/newlib/libc/reent/openr.c
@@ -48,7 +48,7 @@ _open_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _open (file, flags, mode)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/readr.c b/newlib/libc/reent/readr.c
index 7fccefd32..b2ae300d3 100644
--- a/newlib/libc/reent/readr.c
+++ b/newlib/libc/reent/readr.c
@@ -47,7 +47,7 @@ _read_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = (_ssize_t)_read (fd, buf, cnt)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/renamer.c b/newlib/libc/reent/renamer.c
index 5420dc4a0..7e1e111a8 100644
--- a/newlib/libc/reent/renamer.c
+++ b/newlib/libc/reent/renamer.c
@@ -49,7 +49,7 @@ _rename_r (struct _reent *ptr,
 #ifdef HAVE_RENAME
   errno = 0;
   if ((ret = _rename (old, new)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
 #else
   if (_link_r (ptr, old, new) == -1)
     return -1;
diff --git a/newlib/libc/reent/sbrkr.c b/newlib/libc/reent/sbrkr.c
index 21c4bd913..ec948fe6b 100644
--- a/newlib/libc/reent/sbrkr.c
+++ b/newlib/libc/reent/sbrkr.c
@@ -49,7 +49,7 @@ _sbrk_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = (char *)(_sbrk (incr))) == (void *) -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/signalr.c b/newlib/libc/reent/signalr.c
index 345910e4b..863ae7400 100644
--- a/newlib/libc/reent/signalr.c
+++ b/newlib/libc/reent/signalr.c
@@ -51,7 +51,7 @@ _kill_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _kill (pid, sig)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/stat64r.c b/newlib/libc/reent/stat64r.c
index b64736ef2..160d08e33 100644
--- a/newlib/libc/reent/stat64r.c
+++ b/newlib/libc/reent/stat64r.c
@@ -53,7 +53,7 @@ _stat64_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _stat64 (file, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/statr.c b/newlib/libc/reent/statr.c
index 9388e0246..99453e789 100644
--- a/newlib/libc/reent/statr.c
+++ b/newlib/libc/reent/statr.c
@@ -53,7 +53,7 @@ _stat_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _stat (file, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/unlinkr.c b/newlib/libc/reent/unlinkr.c
index 41bac0194..495e65b02 100644
--- a/newlib/libc/reent/unlinkr.c
+++ b/newlib/libc/reent/unlinkr.c
@@ -45,7 +45,7 @@ _unlink_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _unlink (file)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/writer.c b/newlib/libc/reent/writer.c
index 704aba18b..ac2217c57 100644
--- a/newlib/libc/reent/writer.c
+++ b/newlib/libc/reent/writer.c
@@ -47,7 +47,7 @@ _write_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = (_ssize_t)_write (fd, buf, cnt)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/signal/signal.c b/newlib/libc/signal/signal.c
index 0110287d8..a249e9f00 100644
--- a/newlib/libc/signal/signal.c
+++ b/newlib/libc/signal/signal.c
@@ -115,7 +115,7 @@ _signal_r (struct _reent *ptr,
 
   if (sig < 0 || sig >= NSIG)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return SIG_ERR;
     }
 
@@ -136,7 +136,7 @@ _raise_r (struct _reent *ptr,
 
   if (sig < 0 || sig >= NSIG)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return -1;
     }
 
@@ -151,7 +151,7 @@ _raise_r (struct _reent *ptr,
     return 0;
   else if (func == SIG_ERR)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return 1;
     }
   else
diff --git a/newlib/libc/stdio/asniprintf.c b/newlib/libc/stdio/asniprintf.c
index 0bfe00d9b..97e77748e 100644
--- a/newlib/libc/stdio/asniprintf.c
+++ b/newlib/libc/stdio/asniprintf.c
@@ -42,7 +42,7 @@ _asniprintf_r (struct _reent *ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
@@ -88,7 +88,7 @@ asniprintf (char *buf,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/asnprintf.c b/newlib/libc/stdio/asnprintf.c
index f657f9ec9..f82556143 100644
--- a/newlib/libc/stdio/asnprintf.c
+++ b/newlib/libc/stdio/asnprintf.c
@@ -42,7 +42,7 @@ _asnprintf_r (struct _reent *__restrict ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
@@ -94,7 +94,7 @@ asnprintf (char *__restrict buf,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/fdopen.c b/newlib/libc/stdio/fdopen.c
index ef942c284..3481154d1 100644
--- a/newlib/libc/stdio/fdopen.c
+++ b/newlib/libc/stdio/fdopen.c
@@ -73,7 +73,7 @@ _fdopen_r (struct _reent *ptr,
   fdmode = fdflags & O_ACCMODE;
   if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
     {
-      ptr->_errno = EBADF;
+      _REENT_ERRNO(ptr) = EBADF;
       return 0;
     }
 #endif
diff --git a/newlib/libc/stdio/fflush.c b/newlib/libc/stdio/fflush.c
index bbec4a19b..d51e90153 100644
--- a/newlib/libc/stdio/fflush.c
+++ b/newlib/libc/stdio/fflush.c
@@ -137,8 +137,8 @@ __sflush_r (struct _reent *ptr,
 	  /* Save last errno and set errno to 0, so we can check if a device
 	     returns with a valid position -1.  We restore the last errno if
 	     no other error condition has been encountered. */
-	  tmp_errno = ptr->_errno;
-	  ptr->_errno = 0;
+	  tmp_errno = _REENT_ERRNO(ptr);
+	  _REENT_ERRNO(ptr) = 0;
 	  /* Get the physical position we are at in the file.  */
 	  if (fp->_flags & __SOFF)
 	    curoff = fp->_offset;
@@ -152,13 +152,13 @@ __sflush_r (struct _reent *ptr,
 	      else
 #endif
 		curoff = fp->_seek (ptr, fp->_cookie, 0, SEEK_CUR);
-	      if (curoff == -1L && ptr->_errno != 0)
+	      if (curoff == -1L && _REENT_ERRNO(ptr) != 0)
 		{
 		  int result = EOF;
-		  if (ptr->_errno == ESPIPE || ptr->_errno == EINVAL)
+		  if (_REENT_ERRNO(ptr) == ESPIPE || _REENT_ERRNO(ptr) == EINVAL)
 		    {
 		      result = 0;
-		      ptr->_errno = tmp_errno;
+		      _REENT_ERRNO(ptr) = tmp_errno;
 		    }
 		  else
 		    fp->_flags |= __SERR;
@@ -180,8 +180,8 @@ __sflush_r (struct _reent *ptr,
 	  else
 #endif
 	    curoff = fp->_seek (ptr, fp->_cookie, curoff, SEEK_SET);
-	  if (curoff != -1 || ptr->_errno == 0
-	      || ptr->_errno == ESPIPE || ptr->_errno == EINVAL)
+	  if (curoff != -1 || _REENT_ERRNO(ptr) == 0
+	      || _REENT_ERRNO(ptr) == ESPIPE || _REENT_ERRNO(ptr) == EINVAL)
 	    {
 	      /* Seek successful or ignorable error condition.
 		 We can clear read buffer now.  */
@@ -190,9 +190,9 @@ __sflush_r (struct _reent *ptr,
 #endif
 	      fp->_r = 0;
 	      fp->_p = fp->_bf._base;
-	      if ((fp->_flags & __SOFF) && (curoff != -1 || ptr->_errno == 0))
+	      if ((fp->_flags & __SOFF) && (curoff != -1 || _REENT_ERRNO(ptr) == 0))
 		fp->_offset = curoff;
-	      ptr->_errno = tmp_errno;
+	      _REENT_ERRNO(ptr) = tmp_errno;
 	      if (HASUB (fp))
 		FREEUB (ptr, fp);
 	    }
diff --git a/newlib/libc/stdio/fileno.c b/newlib/libc/stdio/fileno.c
index 10fc6e283..4faa6a843 100644
--- a/newlib/libc/stdio/fileno.c
+++ b/newlib/libc/stdio/fileno.c
@@ -73,7 +73,7 @@ fileno (FILE * f)
   else
     {
       result = -1;
-      _REENT->_errno = EBADF;
+      _REENT_ERRNO(_REENT) = EBADF;
     }
   _newlib_flockfile_end (f);
   return result;
diff --git a/newlib/libc/stdio/fileno_u.c b/newlib/libc/stdio/fileno_u.c
index 830a07d2b..f3940e885 100644
--- a/newlib/libc/stdio/fileno_u.c
+++ b/newlib/libc/stdio/fileno_u.c
@@ -39,7 +39,7 @@ fileno_unlocked (FILE * f)
   else
     {
       result = -1;
-      _REENT->_errno = EBADF;
+      _REENT_ERRNO(_REENT) = EBADF;
     }
   return result;
 }
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index ee991ed24..c7a4a941e 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -179,7 +179,7 @@ __sfp (struct _reent *d)
 	break;
     }
   _newlib_sfp_lock_exit ();
-  d->_errno = ENOMEM;
+  _REENT_ERRNO(d) = ENOMEM;
   return NULL;
 
 found:
diff --git a/newlib/libc/stdio/flags.c b/newlib/libc/stdio/flags.c
index 7bbd50181..d686fa046 100644
--- a/newlib/libc/stdio/flags.c
+++ b/newlib/libc/stdio/flags.c
@@ -56,7 +56,7 @@ __sflags (struct _reent *ptr,
       o = O_CREAT | O_APPEND;
       break;
     default:			/* illegal mode */
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return (0);
     }
   while (*++mode)
diff --git a/newlib/libc/stdio/fmemopen.c b/newlib/libc/stdio/fmemopen.c
index 0d043520e..886ba7efe 100644
--- a/newlib/libc/stdio/fmemopen.c
+++ b/newlib/libc/stdio/fmemopen.c
@@ -148,7 +148,7 @@ fmemwriter (struct _reent *ptr,
     memcpy (c->buf + c->pos - n, buf, n - adjust);
   else
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       return EOF;
     }
   return n;
@@ -175,18 +175,18 @@ fmemseeker (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       offset = -1;
     }
   else if (offset > c->max)
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       offset = -1;
     }
 #ifdef __LARGE64_FILES
   else if ((_fpos_t) offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -224,12 +224,12 @@ fmemseeker64 (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       offset = -1;
     }
   else if (offset > c->max)
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       offset = -1;
     }
   else
@@ -277,7 +277,7 @@ _fmemopen_r (struct _reent *ptr,
     return NULL;
   if (!size || !(buf || flags & __SRW))
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/fopencookie.c b/newlib/libc/stdio/fopencookie.c
index 0861528e0..ab406a866 100644
--- a/newlib/libc/stdio/fopencookie.c
+++ b/newlib/libc/stdio/fopencookie.c
@@ -107,7 +107,7 @@ fcreader (struct _reent *ptr,
   fccookie *c = (fccookie *) cookie;
   errno = 0;
   if ((result = c->readfn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 
@@ -129,7 +129,7 @@ fcwriter (struct _reent *ptr,
     }
   errno = 0;
   if ((result = c->writefn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 
@@ -148,11 +148,11 @@ fcseeker (struct _reent *ptr,
 
   errno = 0;
   if (c->seekfn (c->cookie, &offset, whence) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
 #ifdef __LARGE64_FILES
   else if ((_fpos_t)offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -170,7 +170,7 @@ fcseeker64 (struct _reent *ptr,
   fccookie *c = (fccookie *) cookie;
   errno = 0;
   if (c->seekfn (c->cookie, &offset, whence) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return (_fpos64_t) offset;
 }
 #endif /* __LARGE64_FILES */
@@ -185,7 +185,7 @@ fccloser (struct _reent *ptr,
     {
       errno = 0;
       if ((result = c->closefn (c->cookie)) < 0 && errno)
-	ptr->_errno = errno;
+	_REENT_ERRNO(ptr) = errno;
     }
   _free_r (ptr, c);
   return result;
@@ -207,7 +207,7 @@ _fopencookie_r (struct _reent *ptr,
   if (((flags & (__SRD | __SRW)) && !functions.read)
       || ((flags & (__SWR | __SRW)) && !functions.write))
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/fpurge.c b/newlib/libc/stdio/fpurge.c
index 2e4f61ec9..93ac0e2a9 100644
--- a/newlib/libc/stdio/fpurge.c
+++ b/newlib/libc/stdio/fpurge.c
@@ -72,7 +72,7 @@ _fpurge_r (struct _reent *ptr,
   t = fp->_flags;
   if (!t)
     {
-      ptr->_errno = EBADF;
+      _REENT_ERRNO(ptr) = EBADF;
       _newlib_flockfile_exit (fp);
       return EOF;
     }
diff --git a/newlib/libc/stdio/freopen.c b/newlib/libc/stdio/freopen.c
index 29ba5b7ff..eb742c1da 100644
--- a/newlib/libc/stdio/freopen.c
+++ b/newlib/libc/stdio/freopen.c
@@ -137,7 +137,7 @@ _freopen_r (struct _reent *ptr,
   if (file != NULL)
     {
       f = _open_r (ptr, (char *) file, oflags, 0666);
-      e = ptr->_errno;
+      e = _REENT_ERRNO(ptr);
     }
   else
     {
@@ -205,7 +205,7 @@ _freopen_r (struct _reent *ptr,
     {				/* did not get it after all */
       __sfp_lock_acquire ();
       fp->_flags = 0;		/* set it free */
-      ptr->_errno = e;		/* restore in case _close clobbered */
+      _REENT_ERRNO(ptr) = e;		/* restore in case _close clobbered */
       if (!(oflags2 & __SNLK))
 	_funlockfile (fp);
 #ifndef __SINGLE_THREAD__
diff --git a/newlib/libc/stdio/fseeko.c b/newlib/libc/stdio/fseeko.c
index 13df28bfc..6fcc8ef06 100644
--- a/newlib/libc/stdio/fseeko.c
+++ b/newlib/libc/stdio/fseeko.c
@@ -128,7 +128,7 @@ _fseeko_r (struct _reent *ptr,
 
   if ((seekfn = fp->_seek) == NULL)
     {
-      ptr->_errno = ESPIPE;	/* ??? */
+      _REENT_ERRNO(ptr) = ESPIPE;	/* ??? */
       _newlib_flockfile_exit (fp);
       return EOF;
     }
@@ -178,7 +178,7 @@ _fseeko_r (struct _reent *ptr,
       break;
 
     default:
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       _newlib_flockfile_exit (fp);
       return (EOF);
     }
diff --git a/newlib/libc/stdio/ftell.c b/newlib/libc/stdio/ftell.c
index c540c430f..f68b3afcc 100644
--- a/newlib/libc/stdio/ftell.c
+++ b/newlib/libc/stdio/ftell.c
@@ -92,7 +92,7 @@ _ftell_r (struct _reent *ptr,
   if ((long)pos != pos)
     {
       pos = -1;
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
     }
   return (long)pos;
 }
diff --git a/newlib/libc/stdio/ftello.c b/newlib/libc/stdio/ftello.c
index 3df200cea..d62d48a89 100644
--- a/newlib/libc/stdio/ftello.c
+++ b/newlib/libc/stdio/ftello.c
@@ -96,7 +96,7 @@ _ftello_r (struct _reent * ptr,
 
   if (fp->_seek == NULL)
     {
-      ptr->_errno = ESPIPE;
+      _REENT_ERRNO(ptr) = ESPIPE;
       _newlib_flockfile_exit (fp);
       return (_off_t) -1;
     }
diff --git a/newlib/libc/stdio/funopen.c b/newlib/libc/stdio/funopen.c
index a1c0d767d..4ed50196e 100644
--- a/newlib/libc/stdio/funopen.c
+++ b/newlib/libc/stdio/funopen.c
@@ -113,7 +113,7 @@ funreader (struct _reent *ptr,
   funcookie *c = (funcookie *) cookie;
   errno = 0;
   if ((result = c->readfn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 
@@ -127,7 +127,7 @@ funwriter (struct _reent *ptr,
   funcookie *c = (funcookie *) cookie;
   errno = 0;
   if ((result = c->writefn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 
@@ -142,15 +142,15 @@ funseeker (struct _reent *ptr,
   fpos_t result;
   errno = 0;
   if ((result = c->seekfn (c->cookie, (fpos_t) off, whence)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
 #else /* __LARGE64_FILES */
   _fpos64_t result;
   errno = 0;
   if ((result = c->seekfn (c->cookie, (_fpos64_t) off, whence)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   else if ((_fpos_t)result != result)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       result = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -168,7 +168,7 @@ funseeker64 (struct _reent *ptr,
   funcookie *c = (funcookie *) cookie;
   errno = 0;
   if ((result = c->seekfn (c->cookie, off, whence)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 #endif /* __LARGE64_FILES */
@@ -183,7 +183,7 @@ funcloser (struct _reent *ptr,
     {
       errno = 0;
       if ((result = c->closefn (c->cookie)) < 0 && errno)
-	ptr->_errno = errno;
+	_REENT_ERRNO(ptr) = errno;
     }
   _free_r (ptr, c);
   return result;
@@ -202,7 +202,7 @@ _funopen_r (struct _reent *ptr,
 
   if (!readfn && !writefn)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/fvwrite.c b/newlib/libc/stdio/fvwrite.c
index 587ffacd7..34361cb8d 100644
--- a/newlib/libc/stdio/fvwrite.c
+++ b/newlib/libc/stdio/fvwrite.c
@@ -145,7 +145,7 @@ __sfvwrite_r (struct _reent *ptr,
 		      str = (unsigned char *)_malloc_r (ptr, newsize);
 		      if (!str)
 			{
-			  ptr->_errno = ENOMEM;
+			  _REENT_ERRNO(ptr) = ENOMEM;
 			  goto err;
 			}
 		      memcpy (str, fp->_bf._base, curpos);
@@ -162,7 +162,7 @@ __sfvwrite_r (struct _reent *ptr,
 			  _free_r (ptr, fp->_bf._base);
 			  fp->_flags &=  ~__SMBF;
 			  /* Ensure correct errno, even if free changed it.  */
-			  ptr->_errno = ENOMEM;
+			  _REENT_ERRNO(ptr) = ENOMEM;
 			  goto err;
 			}
 		    }
diff --git a/newlib/libc/stdio/mktemp.c b/newlib/libc/stdio/mktemp.c
index 3514818a6..265968198 100644
--- a/newlib/libc/stdio/mktemp.c
+++ b/newlib/libc/stdio/mktemp.c
@@ -159,7 +159,7 @@ _gettemp (struct _reent *ptr,
     continue;
   if (trv - path < suffixlen)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return 0;
     }
   trv -= suffixlen;
@@ -171,7 +171,7 @@ _gettemp (struct _reent *ptr,
     }
   if (end - trv < 6)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return 0;
     }
 
@@ -195,7 +195,7 @@ _gettemp (struct _reent *ptr,
 	    return (0);
 	  if (!(sbuf.st_mode & S_IFDIR))
 	    {
-	      ptr->_errno = ENOTDIR;
+	      _REENT_ERRNO(ptr) = ENOTDIR;
 	      return (0);
 	    }
 	  *trv = '/';
@@ -211,10 +211,10 @@ _gettemp (struct _reent *ptr,
 #ifdef HAVE_MKDIR
 	  if (_mkdir_r (ptr, path, 0700) == 0)
 	    return 1;
-	  if (ptr->_errno != EEXIST)
+	  if (_REENT_ERRNO(ptr) != EEXIST)
 	    return 0;
 #else /* !HAVE_MKDIR */
-	  ptr->_errno = ENOSYS;
+	  _REENT_ERRNO(ptr) = ENOSYS;
 	  return 0;
 #endif /* !HAVE_MKDIR */
 	}
@@ -225,7 +225,7 @@ _gettemp (struct _reent *ptr,
 	  if ((*doopen = _open_r (ptr, path, O_CREAT | O_EXCL | O_RDWR | flags,
 				  0600)) >= 0)
 	    return 1;
-	  if (ptr->_errno != EEXIST)
+	  if (_REENT_ERRNO(ptr) != EEXIST)
 	    return 0;
 	}
 #ifdef __USE_INTERNAL_STAT64
@@ -233,7 +233,7 @@ _gettemp (struct _reent *ptr,
 #else
       else if (_stat_r (ptr, path, &sbuf))
 #endif
-	return (ptr->_errno == ENOENT ? 1 : 0);
+	return (_REENT_ERRNO(ptr) == ENOENT ? 1 : 0);
 
       /* tricky little algorithm for backward compatibility */
       for (trv = start;;)
diff --git a/newlib/libc/stdio/nano-vfprintf.c b/newlib/libc/stdio/nano-vfprintf.c
index 838804eb9..0d42a940f 100644
--- a/newlib/libc/stdio/nano-vfprintf.c
+++ b/newlib/libc/stdio/nano-vfprintf.c
@@ -201,7 +201,7 @@ __ssputs_r (struct _reent *ptr,
 	  str = (unsigned char *)_malloc_r (ptr, newsize);
 	  if (!str)
 	    {
-	      ptr->_errno = ENOMEM;
+	      _REENT_ERRNO(ptr) = ENOMEM;
 	      goto err;
 	    }
 	  memcpy (str, fp->_bf._base, curpos);
@@ -215,7 +215,7 @@ __ssputs_r (struct _reent *ptr,
 	      /* Free unneeded buffer.  */
 	      _free_r (ptr, fp->_bf._base);
 	      /* Ensure correct errno, even if free changed it.  */
-	      ptr->_errno = ENOMEM;
+	      _REENT_ERRNO(ptr) = ENOMEM;
 	      goto err;
 	    }
 	}
@@ -291,7 +291,7 @@ __ssprint_r (struct _reent *ptr,
 	      str = (unsigned char *)_malloc_r (ptr, newsize);
 	      if (!str)
 		{
-		  ptr->_errno = ENOMEM;
+		  _REENT_ERRNO(ptr) = ENOMEM;
 		  goto err;
 		}
 	      memcpy (str, fp->_bf._base, curpos);
@@ -306,7 +306,7 @@ __ssprint_r (struct _reent *ptr,
 		  /* Free unneeded buffer.  */
 		  _free_r (ptr, fp->_bf._base);
 		  /* Ensure correct errno, even if free changed it.  */
-		  ptr->_errno = ENOMEM;
+		  _REENT_ERRNO(ptr) = ENOMEM;
 		  goto err;
 		}
 	    }
@@ -501,7 +501,7 @@ _VFPRINTF_R (struct _reent *data,
       fp->_bf._base = fp->_p = _malloc_r (data, 64);
       if (!fp->_p)
 	{
-	  data->_errno = ENOMEM;
+	  _REENT_ERRNO(data) = ENOMEM;
 	  return EOF;
 	}
       fp->_bf._size = 64;
diff --git a/newlib/libc/stdio/open_memstream.c b/newlib/libc/stdio/open_memstream.c
index ed71b1cdc..e3a52ed33 100644
--- a/newlib/libc/stdio/open_memstream.c
+++ b/newlib/libc/stdio/open_memstream.c
@@ -105,7 +105,7 @@ memwriter (struct _reent *ptr,
      big that user cannot do ftello.  */
   if (sizeof (OFF_T) == sizeof (size_t) && (ssize_t) (c->pos + n) < 0)
     {
-      ptr->_errno = EFBIG;
+      _REENT_ERRNO(ptr) = EFBIG;
       return EOF;
     }
   /* Grow the buffer, if necessary.  Choose a geometric growth factor
@@ -160,18 +160,18 @@ memseeker (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       offset = -1;
     }
   else if ((size_t) offset != offset)
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       offset = -1;
     }
 #ifdef __LARGE64_FILES
   else if ((_fpos_t) offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -227,12 +227,12 @@ memseeker64 (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       offset = -1;
     }
   else if ((size_t) offset != offset)
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       offset = -1;
     }
   else
@@ -301,7 +301,7 @@ internal_open_memstream_r (struct _reent *ptr,
 
   if (!buf || !size)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/perror.c b/newlib/libc/stdio/perror.c
index eda7e993b..2033a6f2f 100644
--- a/newlib/libc/stdio/perror.c
+++ b/newlib/libc/stdio/perror.c
@@ -90,7 +90,7 @@ _perror_r (struct _reent *ptr,
       WRITE_STR (": ");
     }
 
-  if ((error = _strerror_r (ptr, ptr->_errno, 1, &dummy)) != NULL)
+  if ((error = _strerror_r (ptr, _REENT_ERRNO(ptr), 1, &dummy)) != NULL)
     WRITE_STR (error);
 
 #ifdef __SCLE
diff --git a/newlib/libc/stdio/refill.c b/newlib/libc/stdio/refill.c
index 8516d8576..8584906ea 100644
--- a/newlib/libc/stdio/refill.c
+++ b/newlib/libc/stdio/refill.c
@@ -58,7 +58,7 @@ __srefill_r (struct _reent * ptr,
     {
       if ((fp->_flags & __SRW) == 0)
 	{
-	  ptr->_errno = EBADF;
+	  _REENT_ERRNO(ptr) = EBADF;
 	  fp->_flags |= __SERR;
 	  return EOF;
 	}
diff --git a/newlib/libc/stdio/sniprintf.c b/newlib/libc/stdio/sniprintf.c
index 375a3971f..b05ffcb12 100644
--- a/newlib/libc/stdio/sniprintf.c
+++ b/newlib/libc/stdio/sniprintf.c
@@ -38,7 +38,7 @@ _sniprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -49,7 +49,7 @@ _sniprintf_r (struct _reent *ptr,
   ret = _svfiprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
@@ -69,7 +69,7 @@ sniprintf (char *str,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -80,7 +80,7 @@ sniprintf (char *str,
   ret = _svfiprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
diff --git a/newlib/libc/stdio/snprintf.c b/newlib/libc/stdio/snprintf.c
index 50d7ddd11..4a3084bea 100644
--- a/newlib/libc/stdio/snprintf.c
+++ b/newlib/libc/stdio/snprintf.c
@@ -37,7 +37,7 @@ _snprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -48,7 +48,7 @@ _snprintf_r (struct _reent *ptr,
   ret = _svfprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
@@ -74,7 +74,7 @@ snprintf (char *__restrict str,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -85,7 +85,7 @@ snprintf (char *__restrict str,
   ret = _svfprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
diff --git a/newlib/libc/stdio/swprintf.c b/newlib/libc/stdio/swprintf.c
index ec7e089ca..634b22beb 100644
--- a/newlib/libc/stdio/swprintf.c
+++ b/newlib/libc/stdio/swprintf.c
@@ -564,7 +564,7 @@ _swprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX / sizeof (wchar_t))
     {
-      ptr->_errno = EOVERFLOW;	/* POSIX extension */
+      _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -584,7 +584,7 @@ _swprintf_r (struct _reent *ptr,
     /* _svfwprintf_r() returns how many wide characters it would have printed
      * if there were enough space.  Return an error if too big to fit in str,
      * unlike snprintf, which returns the size needed.  */
-    ptr->_errno = EOVERFLOW;	/* POSIX extension */
+    _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return (ret);
@@ -604,7 +604,7 @@ swprintf (wchar_t *__restrict str,
 
   if (size > INT_MAX / sizeof (wchar_t))
     {
-      ptr->_errno = EOVERFLOW;	/* POSIX extension */
+      _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -624,7 +624,7 @@ swprintf (wchar_t *__restrict str,
     /* _svfwprintf_r() returns how many wide characters it would have printed
      * if there were enough space.  Return an error if too big to fit in str,
      * unlike snprintf, which returns the size needed.  */
-    ptr->_errno = EOVERFLOW;	/* POSIX extension */
+    _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return (ret);
diff --git a/newlib/libc/stdio/tmpfile.c b/newlib/libc/stdio/tmpfile.c
index f209a3edf..5fe6e3bf1 100644
--- a/newlib/libc/stdio/tmpfile.c
+++ b/newlib/libc/stdio/tmpfile.c
@@ -65,15 +65,15 @@ _tmpfile_r (struct _reent *ptr)
       fd = _open_r (ptr, f, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
 		    S_IRUSR | S_IWUSR);
     }
-  while (fd < 0 && ptr->_errno == EEXIST);
+  while (fd < 0 && _REENT_ERRNO(ptr) == EEXIST);
   if (fd < 0)
     return NULL;
   fp = _fdopen_r (ptr, fd, "wb+");
-  e = ptr->_errno;
+  e = _REENT_ERRNO(ptr);
   if (!fp)
     _close_r (ptr, fd);
   (void) _remove_r (ptr, f);
-  ptr->_errno = e;
+  _REENT_ERRNO(ptr) = e;
   return fp;
 }
 
diff --git a/newlib/libc/stdio/tmpnam.c b/newlib/libc/stdio/tmpnam.c
index 3bd5b6a58..7379a7640 100644
--- a/newlib/libc/stdio/tmpnam.c
+++ b/newlib/libc/stdio/tmpnam.c
@@ -104,7 +104,7 @@ worker (struct _reent *ptr,
       t = _open_r (ptr, result, O_RDONLY, 0);
       if (t == -1)
 	{
-	  if (ptr->_errno == ENOSYS)
+	  if (_REENT_ERRNO(ptr) == ENOSYS)
 	    {
 	      result[0] = '\0';
 	      return 0;
diff --git a/newlib/libc/stdio/vasniprintf.c b/newlib/libc/stdio/vasniprintf.c
index edfbd2242..b24cd96fb 100644
--- a/newlib/libc/stdio/vasniprintf.c
+++ b/newlib/libc/stdio/vasniprintf.c
@@ -42,7 +42,7 @@ _vasniprintf_r (struct _reent *ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/vasnprintf.c b/newlib/libc/stdio/vasnprintf.c
index 1fbf5a171..b3787ec7a 100644
--- a/newlib/libc/stdio/vasnprintf.c
+++ b/newlib/libc/stdio/vasnprintf.c
@@ -42,7 +42,7 @@ _vasnprintf_r (struct _reent *ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/vfprintf.c b/newlib/libc/stdio/vfprintf.c
index 32ebb1468..6a198e2c6 100644
--- a/newlib/libc/stdio/vfprintf.c
+++ b/newlib/libc/stdio/vfprintf.c
@@ -222,7 +222,7 @@ __ssputs_r (struct _reent *ptr,
 			str = (unsigned char *)_malloc_r (ptr, newsize);
 			if (!str)
 			{
-				ptr->_errno = ENOMEM;
+				_REENT_ERRNO(ptr) = ENOMEM;
 				goto err;
 			}
 			memcpy (str, fp->_bf._base, curpos);
@@ -237,7 +237,7 @@ __ssputs_r (struct _reent *ptr,
 				_free_r (ptr, fp->_bf._base);
 				/* Ensure correct errno, even if free
 				 * changed it.  */
-				ptr->_errno = ENOMEM;
+				_REENT_ERRNO(ptr) = ENOMEM;
 				goto err;
 			}
 		}
@@ -306,7 +306,7 @@ __ssprint_r (struct _reent *ptr,
 				str = (unsigned char *)_malloc_r (ptr, newsize);
 				if (!str)
 				{
-					ptr->_errno = ENOMEM;
+					_REENT_ERRNO(ptr) = ENOMEM;
 					goto err;
 				}
 				memcpy (str, fp->_bf._base, curpos);
@@ -321,7 +321,7 @@ __ssprint_r (struct _reent *ptr,
 					_free_r (ptr, fp->_bf._base);
 					/* Ensure correct errno, even if free
 					 * changed it.  */
-					ptr->_errno = ENOMEM;
+					_REENT_ERRNO(ptr) = ENOMEM;
 					goto err;
 				}
 			}
@@ -868,7 +868,7 @@ _VFPRINTF_R (struct _reent *data,
 		fp->_bf._base = fp->_p = _malloc_r (data, 64);
 		if (!fp->_p)
 		{
-			data->_errno = ENOMEM;
+			_REENT_ERRNO(data) = ENOMEM;
 			return EOF;
 		}
 		fp->_bf._size = 64;
@@ -1374,7 +1374,7 @@ reswitch:	switch (ch) {
 		case 'm':  /* extension */
 			{
 				int dummy;
-				cp = _strerror_r (data, data->_errno, 1, &dummy);
+				cp = _strerror_r (data, _REENT_ERRNO(data), 1, &dummy);
 			}
 			flags &= ~LONGINT;
 			goto string;
diff --git a/newlib/libc/stdio/vfscanf.c b/newlib/libc/stdio/vfscanf.c
index 037692531..cfeea9876 100644
--- a/newlib/libc/stdio/vfscanf.c
+++ b/newlib/libc/stdio/vfscanf.c
@@ -774,7 +774,7 @@ __SVFSCANF_R (struct _reent *rptr,
 	      width = 0;
 	      goto again;
 	    }
-	  rptr->_errno = EINVAL;
+	  _REENT_ERRNO(rptr) = EINVAL;
 	  goto input_failure;
 #endif /* !_NO_POS_ARGS */
 
diff --git a/newlib/libc/stdio/vfwprintf.c b/newlib/libc/stdio/vfwprintf.c
index 7384b37d3..7807a1229 100644
--- a/newlib/libc/stdio/vfwprintf.c
+++ b/newlib/libc/stdio/vfwprintf.c
@@ -611,7 +611,7 @@ _VFWPRINTF_R (struct _reent *data,
 		fp->_bf._base = fp->_p = _malloc_r (data, 64);
 		if (!fp->_p)
 		{
-			data->_errno = ENOMEM;
+			_REENT_ERRNO(data) = ENOMEM;
 			return EOF;
 		}
 		fp->_bf._size = 64;
@@ -1119,7 +1119,7 @@ reswitch:	switch (ch) {
 		case L'm':  /* GNU extension */
 			{
 				int dummy;
-				cp = (wchar_t *) _strerror_r (data, data->_errno, 1, &dummy);
+				cp = (wchar_t *) _strerror_r (data, _REENT_ERRNO(data), 1, &dummy);
 			}
 			flags &= ~LONGINT;
 			goto string;
diff --git a/newlib/libc/stdio/vfwscanf.c b/newlib/libc/stdio/vfwscanf.c
index e9e00dfec..df966f929 100644
--- a/newlib/libc/stdio/vfwscanf.c
+++ b/newlib/libc/stdio/vfwscanf.c
@@ -682,7 +682,7 @@ __SVFWSCANF_R (struct _reent *rptr,
 	      width = 0;
 	      goto again;
 	    }
-	  rptr->_errno = EINVAL;
+	  _REENT_ERRNO(rptr) = EINVAL;
 	  goto input_failure;
 #endif /* !_NO_POS_ARGS */
 
diff --git a/newlib/libc/stdio/vsniprintf.c b/newlib/libc/stdio/vsniprintf.c
index a8b272e2f..e7625ffd5 100644
--- a/newlib/libc/stdio/vsniprintf.c
+++ b/newlib/libc/stdio/vsniprintf.c
@@ -54,7 +54,7 @@ _vsniprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -63,7 +63,7 @@ _vsniprintf_r (struct _reent *ptr,
   f._file = -1;  /* No file. */
   ret = _svfiprintf_r (ptr, &f, fmt, ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return ret;
diff --git a/newlib/libc/stdio/vsnprintf.c b/newlib/libc/stdio/vsnprintf.c
index 5c617a8a3..ecf82eaef 100644
--- a/newlib/libc/stdio/vsnprintf.c
+++ b/newlib/libc/stdio/vsnprintf.c
@@ -60,7 +60,7 @@ _vsnprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -69,7 +69,7 @@ _vsnprintf_r (struct _reent *ptr,
   f._file = -1;  /* No file. */
   ret = _svfprintf_r (ptr, &f, fmt, ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return ret;
diff --git a/newlib/libc/stdio/vswprintf.c b/newlib/libc/stdio/vswprintf.c
index 89795ed8d..08a8545ba 100644
--- a/newlib/libc/stdio/vswprintf.c
+++ b/newlib/libc/stdio/vswprintf.c
@@ -42,7 +42,7 @@ _vswprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX / sizeof (wchar_t))
     {
-      ptr->_errno = EOVERFLOW;	/* POSIX extension */
+      _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -60,7 +60,7 @@ _vswprintf_r (struct _reent *ptr,
     /* _svfwprintf_r() returns how many wide characters it would have printed
      * if there were enough space.  Return an error if too big to fit in str,
      * unlike snprintf, which returns the size needed.  */
-    ptr->_errno = EOVERFLOW;	/* POSIX extension */
+    _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return ret;
diff --git a/newlib/libc/stdio/wsetup.c b/newlib/libc/stdio/wsetup.c
index 6a820f1c0..0fd9690aa 100644
--- a/newlib/libc/stdio/wsetup.c
+++ b/newlib/libc/stdio/wsetup.c
@@ -45,7 +45,7 @@ __swsetup_r (struct _reent *ptr,
     {
       if ((fp->_flags & __SRW) == 0)
         {
-	  ptr->_errno = EBADF;
+	  _REENT_ERRNO(ptr) = EBADF;
 	  fp->_flags |= __SERR;
 	  return EOF;
         }
diff --git a/newlib/libc/stdio64/fdopen64.c b/newlib/libc/stdio64/fdopen64.c
index 9d9645b36..d93b3d4d8 100644
--- a/newlib/libc/stdio64/fdopen64.c
+++ b/newlib/libc/stdio64/fdopen64.c
@@ -55,7 +55,7 @@ _fdopen64_r (struct _reent *ptr,
   fdmode = fdflags & O_ACCMODE;
   if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
     {
-      ptr->_errno = EBADF;
+      _REENT_ERRNO(ptr) = EBADF;
       return 0;
     }
 #endif
diff --git a/newlib/libc/stdio64/freopen64.c b/newlib/libc/stdio64/freopen64.c
index e6ba64f7d..5e0c32c37 100644
--- a/newlib/libc/stdio64/freopen64.c
+++ b/newlib/libc/stdio64/freopen64.c
@@ -138,7 +138,7 @@ _freopen64_r (struct _reent *ptr,
   if (file != NULL)
     {
       f = _open64_r (ptr, (char *) file, oflags, 0666);
-      e = ptr->_errno;
+      e = _REENT_ERRNO(ptr);
     }
   else
     {
@@ -206,7 +206,7 @@ _freopen64_r (struct _reent *ptr,
     {				/* did not get it after all */
       __sfp_lock_acquire ();
       fp->_flags = 0;		/* set it free */
-      ptr->_errno = e;		/* restore in case _close clobbered */
+      _REENT_ERRNO(ptr) = e;		/* restore in case _close clobbered */
       if (!(oflags2 & __SNLK))
 	_funlockfile (fp);
 #ifndef __SINGLE_THREAD__
diff --git a/newlib/libc/stdio64/fseeko64.c b/newlib/libc/stdio64/fseeko64.c
index 3087bef9e..c5b30aed0 100644
--- a/newlib/libc/stdio64/fseeko64.c
+++ b/newlib/libc/stdio64/fseeko64.c
@@ -102,7 +102,7 @@ _fseeko64_r (struct _reent *ptr,
     {
       if ((_off_t) offset != offset)
 	{
-	  ptr->_errno = EOVERFLOW;
+	  _REENT_ERRNO(ptr) = EOVERFLOW;
 	  return EOF;
 	}
       return (_off64_t) _fseeko_r (ptr, fp, offset, whence);
@@ -129,7 +129,7 @@ _fseeko64_r (struct _reent *ptr,
 
   if ((seekfn = fp->_seek64) == NULL)
     {
-      ptr->_errno = ESPIPE;	/* ??? */
+      _REENT_ERRNO(ptr) = ESPIPE;	/* ??? */
       _newlib_flockfile_exit(fp);
       return EOF;
     }
@@ -179,7 +179,7 @@ _fseeko64_r (struct _reent *ptr,
       break;
 
     default:
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       _newlib_flockfile_exit(fp);
       return (EOF);
     }
diff --git a/newlib/libc/stdio64/ftello64.c b/newlib/libc/stdio64/ftello64.c
index 6fb76c3ef..cd00def46 100644
--- a/newlib/libc/stdio64/ftello64.c
+++ b/newlib/libc/stdio64/ftello64.c
@@ -93,7 +93,7 @@ _ftello64_r (struct _reent *ptr,
 
   if (fp->_seek64 == NULL)
     {
-      ptr->_errno = ESPIPE;
+      _REENT_ERRNO(ptr) = ESPIPE;
       _newlib_flockfile_exit(fp);
       return (_off64_t) -1;
     }
diff --git a/newlib/libc/stdio64/tmpfile64.c b/newlib/libc/stdio64/tmpfile64.c
index 18a38d65c..35b035c91 100644
--- a/newlib/libc/stdio64/tmpfile64.c
+++ b/newlib/libc/stdio64/tmpfile64.c
@@ -68,15 +68,15 @@ _tmpfile64_r (struct _reent *ptr)
       fd = _open64_r (ptr, f, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
 		      S_IRUSR | S_IWUSR);
   }
-  while (fd < 0 && ptr->_errno == EEXIST);
+  while (fd < 0 && _REENT_ERRNO(ptr) == EEXIST);
   if (fd < 0)
     return NULL;
   fp = _fdopen64_r (ptr, fd, "wb+");
-  e = ptr->_errno;
+  e = _REENT_ERRNO(ptr);
   if (!fp)
     _close_r (ptr, fd);
   (void) _remove_r (ptr, f);
-  ptr->_errno = e;
+  _REENT_ERRNO(ptr) = e;
   return fp;
 }
 
diff --git a/newlib/libc/stdlib/__adjust.c b/newlib/libc/stdlib/__adjust.c
index 4c478f30b..ab6f125b9 100644
--- a/newlib/libc/stdlib/__adjust.c
+++ b/newlib/libc/stdlib/__adjust.c
@@ -21,12 +21,12 @@ __adjust (struct _reent *ptr,
 
   if (dexp > MAXE)
     {
-      ptr->_errno = ERANGE;
+      _REENT_ERRNO(ptr) = ERANGE;
       return (sign) ? -HUGE_VAL : HUGE_VAL;
     }
   else if (dexp < MINE)
     {
-      ptr->_errno = ERANGE;
+      _REENT_ERRNO(ptr) = ERANGE;
       return 0.0;
     }
 
diff --git a/newlib/libc/stdlib/mbrtowc.c b/newlib/libc/stdlib/mbrtowc.c
index 65284a0eb..521b7a5f4 100644
--- a/newlib/libc/stdlib/mbrtowc.c
+++ b/newlib/libc/stdlib/mbrtowc.c
@@ -32,7 +32,7 @@ _mbrtowc_r (struct _reent *ptr,
   if (retval == -1)
     {
       ps->__count = 0;
-      ptr->_errno = EILSEQ;
+      _REENT_ERRNO(ptr) = EILSEQ;
       return (size_t)(-1);
     }
   else
@@ -68,7 +68,7 @@ mbrtowc (wchar_t *__restrict pwc,
   if (retval == -1)
     {
       ps->__count = 0;
-      reent->_errno = EILSEQ;
+      _REENT_ERRNO(reent) = EILSEQ;
       return (size_t)(-1);
     }
   else
diff --git a/newlib/libc/stdlib/mbsnrtowcs.c b/newlib/libc/stdlib/mbsnrtowcs.c
index d3ce25084..8f94b1da5 100644
--- a/newlib/libc/stdlib/mbsnrtowcs.c
+++ b/newlib/libc/stdlib/mbsnrtowcs.c
@@ -126,7 +126,7 @@ _mbsnrtowcs_r (struct _reent *r,
       else
 	{
 	  ps->__count = 0;
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return (size_t)-1;
 	}
     }
diff --git a/newlib/libc/stdlib/mbtowc_r.c b/newlib/libc/stdlib/mbtowc_r.c
index 920a7ea3c..ca876f9a0 100644
--- a/newlib/libc/stdlib/mbtowc_r.c
+++ b/newlib/libc/stdlib/mbtowc_r.c
@@ -39,7 +39,7 @@ __ascii_mbtowc (struct _reent *r,
 #ifdef __CYGWIN__
   if ((wchar_t)*t >= 0x80)
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
 #endif
@@ -117,7 +117,7 @@ ___iso_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
 	  *pwc = __iso_8859_conv[iso_idx][*t - 0xa0];
 	  if (*pwc == 0) /* Invalid character */
 	    {
-	      r->_errno = EILSEQ;
+	      _REENT_ERRNO(r) = EILSEQ;
 	      return -1;
 	    }
 	  return 1;
@@ -290,7 +290,7 @@ ___cp_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
 	  *pwc = __cp_conv[cp_idx][*t - 0x80];
 	  if (*pwc == 0) /* Invalid character */
 	    {
-	      r->_errno = EILSEQ;
+	      _REENT_ERRNO(r) = EILSEQ;
 	      return -1;
 	    }
 	  return 1;
@@ -578,13 +578,13 @@ __utf8_mbtowc (struct _reent *r,
       ch = t[i++];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       if (state->__value.__wchb[0] < 0xc2)
 	{
 	  /* overlong UTF-8 sequence */
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__count = 0;
@@ -607,12 +607,12 @@ __utf8_mbtowc (struct _reent *r,
       if (state->__value.__wchb[0] == 0xe0 && ch < 0xa0)
 	{
 	  /* overlong UTF-8 sequence */
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__value.__wchb[1] = ch;
@@ -625,7 +625,7 @@ __utf8_mbtowc (struct _reent *r,
       ch = t[i++];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__count = 0;
@@ -651,12 +651,12 @@ __utf8_mbtowc (struct _reent *r,
 	  || (state->__value.__wchb[0] == 0xf4 && ch >= 0x90))
 	{
 	  /* overlong UTF-8 sequence or result is > 0x10ffff */
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__value.__wchb[1] = ch;
@@ -669,7 +669,7 @@ __utf8_mbtowc (struct _reent *r,
       ch = (state->__count == 2) ? t[i++] : state->__value.__wchb[2];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__value.__wchb[2] = ch;
@@ -702,7 +702,7 @@ __utf8_mbtowc (struct _reent *r,
       ch = t[i++];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       tmp = (wint_t)((state->__value.__wchb[0] & 0x07) << 18)
@@ -719,7 +719,7 @@ __utf8_mbtowc (struct _reent *r,
       return i;
     }
 
-  r->_errno = EILSEQ;
+  _REENT_ERRNO(r) = EILSEQ;
   return -1;
 }
 
@@ -769,7 +769,7 @@ __sjis_mbtowc (struct _reent *r,
 	}
       else  
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -836,7 +836,7 @@ __eucjp_mbtowc (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -851,7 +851,7 @@ __eucjp_mbtowc (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -955,7 +955,7 @@ __jis_mbtowc (struct _reent *r,
 	  break;
 	case ERROR:
 	default:
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
 
diff --git a/newlib/libc/stdlib/strtod.c b/newlib/libc/stdlib/strtod.c
index cd0222481..9156ed7d6 100644
--- a/newlib/libc/stdlib/strtod.c
+++ b/newlib/libc/stdlib/strtod.c
@@ -595,7 +595,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se,
 			if (e1 > DBL_MAX_10_EXP) {
  ovfl:
 #ifndef NO_ERRNO
-				ptr->_errno = ERANGE;
+				_REENT_ERRNO(ptr) = ERANGE;
 #endif
 				/* Can't trust HUGE_VAL */
 #ifdef IEEE_Arith
@@ -702,7 +702,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se,
  undfl:
 					dval(rv) = 0.;
 #ifndef NO_ERRNO
-					ptr->_errno = ERANGE;
+					_REENT_ERRNO(ptr) = ERANGE;
 #endif
 					if (bd0)
 						goto retfree;
@@ -1249,7 +1249,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se,
 #ifndef NO_ERRNO
 		/* try to avoid the bug of testing an 8087 register value */
 		if ((dword0(rv) & Exp_mask) == 0)
-			ptr->_errno = ERANGE;
+			_REENT_ERRNO(ptr) = ERANGE;
 #endif
 		}
 #endif /* Avoid_Underflow */
@@ -1303,7 +1303,7 @@ strtof_l (const char *__restrict s00, char **__restrict se, locale_t loc)
   float retval = (float) val;
 #ifndef NO_ERRNO
   if (isinf (retval) && !isinf (val))
-    _REENT->_errno = ERANGE;
+    _REENT_ERRNO(_REENT) = ERANGE;
 #endif
   return retval;
 }
@@ -1340,7 +1340,7 @@ strtof (const char *__restrict s00,
   float retval = (float) val;
 #ifndef NO_ERRNO
   if ((isinf (retval) && !isinf (val)) || (isdenormf(retval) && !isdenorm(val)))
-    _REENT->_errno = ERANGE;
+    _REENT_ERRNO(_REENT) = ERANGE;
 #endif
   return retval;
 }
diff --git a/newlib/libc/stdlib/strtoimax.c b/newlib/libc/stdlib/strtoimax.c
index c3f27df2e..a64b7da66 100644
--- a/newlib/libc/stdlib/strtoimax.c
+++ b/newlib/libc/stdlib/strtoimax.c
@@ -136,10 +136,10 @@ _strtoimax_l(struct _reent *rptr, const char * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? INTMAX_MIN : INTMAX_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/strtol.c b/newlib/libc/stdlib/strtol.c
index 6383c27e8..09d4333ed 100644
--- a/newlib/libc/stdlib/strtol.c
+++ b/newlib/libc/stdlib/strtol.c
@@ -204,7 +204,7 @@ _strtol_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_MIN : LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoll.c b/newlib/libc/stdlib/strtoll.c
index 9aa2c747b..0f2fa5315 100644
--- a/newlib/libc/stdlib/strtoll.c
+++ b/newlib/libc/stdlib/strtoll.c
@@ -201,7 +201,7 @@ _strtoll_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_LONG_MIN : LONG_LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoul.c b/newlib/libc/stdlib/strtoul.c
index 4191e43ec..d31bde5d2 100644
--- a/newlib/libc/stdlib/strtoul.c
+++ b/newlib/libc/stdlib/strtoul.c
@@ -178,7 +178,7 @@ _strtoul_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = ULONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoull.c b/newlib/libc/stdlib/strtoull.c
index 10018ca0d..943de6b83 100644
--- a/newlib/libc/stdlib/strtoull.c
+++ b/newlib/libc/stdlib/strtoull.c
@@ -176,7 +176,7 @@ _strtoull_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = ULONG_LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoumax.c b/newlib/libc/stdlib/strtoumax.c
index cf1a427fe..adcf2b338 100644
--- a/newlib/libc/stdlib/strtoumax.c
+++ b/newlib/libc/stdlib/strtoumax.c
@@ -115,10 +115,10 @@ _strtoumax_l(struct _reent *rptr, const char * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = UINTMAX_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/wcrtomb.c b/newlib/libc/stdlib/wcrtomb.c
index 97436cb74..6d670e23a 100644
--- a/newlib/libc/stdlib/wcrtomb.c
+++ b/newlib/libc/stdlib/wcrtomb.c
@@ -31,7 +31,7 @@ _wcrtomb_r (struct _reent *ptr,
   if (retval == -1)
     {
       ps->__count = 0;
-      ptr->_errno = EILSEQ;
+      _REENT_ERRNO(ptr) = EILSEQ;
       return (size_t)(-1);
     }
   else
@@ -67,7 +67,7 @@ wcrtomb (char *__restrict s,
   if (retval == -1)
     {
       ps->__count = 0;
-      reent->_errno = EILSEQ;
+      _REENT_ERRNO(reent) = EILSEQ;
       return (size_t)(-1);
     }
   else
diff --git a/newlib/libc/stdlib/wcsnrtombs.c b/newlib/libc/stdlib/wcsnrtombs.c
index 43dd2f3e6..dfd974f24 100644
--- a/newlib/libc/stdlib/wcsnrtombs.c
+++ b/newlib/libc/stdlib/wcsnrtombs.c
@@ -104,7 +104,7 @@ _wcsnrtombs_l (struct _reent *r, char *dst, const wchar_t **src, size_t nwc,
       int bytes = loc->wctomb (r, buff, *pwcs, ps);
       if (bytes == -1)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  ps->__count = 0;
 	  return (size_t)-1;
 	}
diff --git a/newlib/libc/stdlib/wcstod.c b/newlib/libc/stdlib/wcstod.c
index 375ffe288..7b0983470 100644
--- a/newlib/libc/stdlib/wcstod.c
+++ b/newlib/libc/stdlib/wcstod.c
@@ -257,7 +257,7 @@ wcstof_l (const wchar_t *__restrict nptr, wchar_t **__restrict endptr,
   float retval = (float) val;
 #ifndef NO_ERRNO
   if (isinf (retval) && !isinf (val))
-    _REENT->_errno = ERANGE;
+    _REENT_ERRNO(_REENT) = ERANGE;
 #endif
   return retval;
 }
@@ -272,7 +272,7 @@ wcstof (const wchar_t *__restrict nptr,
   float retval = (float) val;
 #ifndef NO_ERRNO
   if (isinf (retval) && !isinf (val))
-    _REENT->_errno = ERANGE;
+    _REENT_ERRNO(_REENT) = ERANGE;
 #endif
 
   return retval;
diff --git a/newlib/libc/stdlib/wcstoimax.c b/newlib/libc/stdlib/wcstoimax.c
index 02ab1c1f4..0c939c978 100644
--- a/newlib/libc/stdlib/wcstoimax.c
+++ b/newlib/libc/stdlib/wcstoimax.c
@@ -122,10 +122,10 @@ _wcstoimax_l(struct _reent *rptr, const wchar_t * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? INTMAX_MIN : INTMAX_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/wcstol.c b/newlib/libc/stdlib/wcstol.c
index 8b6de3873..2a8c28f97 100644
--- a/newlib/libc/stdlib/wcstol.c
+++ b/newlib/libc/stdlib/wcstol.c
@@ -200,7 +200,7 @@ _wcstol_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_MIN : LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoll.c b/newlib/libc/stdlib/wcstoll.c
index c0e5dc747..34a7e376d 100644
--- a/newlib/libc/stdlib/wcstoll.c
+++ b/newlib/libc/stdlib/wcstoll.c
@@ -200,7 +200,7 @@ _wcstoll_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_LONG_MIN : LONG_LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoul.c b/newlib/libc/stdlib/wcstoul.c
index fe3c87867..9c78fc3b4 100644
--- a/newlib/libc/stdlib/wcstoul.c
+++ b/newlib/libc/stdlib/wcstoul.c
@@ -179,7 +179,7 @@ _wcstoul_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = ULONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoull.c b/newlib/libc/stdlib/wcstoull.c
index 5ac325790..09422fa97 100644
--- a/newlib/libc/stdlib/wcstoull.c
+++ b/newlib/libc/stdlib/wcstoull.c
@@ -150,7 +150,7 @@ _wcstoull_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	register int neg = 0, any, cutlim;
 
 	if(base < 0  ||  base == 1  ||  base > 36)  {
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 		return(0ULL);
 	}
 	/*
@@ -195,7 +195,7 @@ _wcstoull_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = ULLONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoumax.c b/newlib/libc/stdlib/wcstoumax.c
index 17b5275fa..623f5b947 100644
--- a/newlib/libc/stdlib/wcstoumax.c
+++ b/newlib/libc/stdlib/wcstoumax.c
@@ -121,10 +121,10 @@ _wcstoumax_l(struct _reent *rptr,const wchar_t * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = UINTMAX_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/wctomb_r.c b/newlib/libc/stdlib/wctomb_r.c
index b4799341e..a7f87cd9e 100644
--- a/newlib/libc/stdlib/wctomb_r.c
+++ b/newlib/libc/stdlib/wctomb_r.c
@@ -35,7 +35,7 @@ __ascii_wctomb (struct _reent *r,
   if ((size_t)wchar >= 0x100)
 #endif
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
 
@@ -133,7 +133,7 @@ __utf8_wctomb (struct _reent *r,
       return 4;
     }
 
-  r->_errno = EILSEQ;
+  _REENT_ERRNO(r) = EILSEQ;
   return -1;
 }
 
@@ -165,7 +165,7 @@ __sjis_wctomb (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -204,7 +204,7 @@ __eucjp_wctomb (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -244,7 +244,7 @@ __jis_wctomb (struct _reent *r,
 	  *s = (char)char2;
 	  return cnt + 2;
 	}
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   if (state->__state != 0)
@@ -284,14 +284,14 @@ ___iso_wctomb (struct _reent *r, char *s, wchar_t _wchar, int iso_idx,
 		*s = (char) (mb + 0xa0);
 		return 1;
 	      }
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
  
   if ((size_t)wchar >= 0x100)
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
 
@@ -440,14 +440,14 @@ ___cp_wctomb (struct _reent *r, char *s, wchar_t _wchar, int cp_idx,
 		*s = (char) (mb + 0x80);
 		return 1;
 	      }
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
 
   if ((size_t)wchar >= 0x100)
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
 
diff --git a/newlib/libc/string/strerror.c b/newlib/libc/string/strerror.c
index c1b934696..8f961d3b5 100644
--- a/newlib/libc/string/strerror.c
+++ b/newlib/libc/string/strerror.c
@@ -882,7 +882,7 @@ _strerror_r (struct _reent *ptr,
 #endif
     default:
       if (!errptr)
-        errptr = &ptr->_errno;
+        errptr = &_REENT_ERRNO(ptr);
       if ((error = _user_strerror (errnum, internal, errptr)) == 0)
         error = "";
       break;
diff --git a/winsup/cygwin/cygerrno.h b/winsup/cygwin/cygerrno.h
index 940bfa5e0..e1c3f04f7 100644
--- a/winsup/cygwin/cygerrno.h
+++ b/winsup/cygwin/cygerrno.h
@@ -36,7 +36,7 @@ extern inline int
 __set_errno (const char *fn, int ln, int val)
 {
   debug_printf ("%s:%d setting errno %d", fn, ln, val);
-  return errno = _impure_ptr->_errno = val;
+  return errno = _REENT_ERRNO(_impure_ptr) = val;
 }
 #define set_errno(val) __set_errno (__PRETTY_FUNCTION__, __LINE__, (val))
 
@@ -55,7 +55,7 @@ class save_errno
     save_errno (int what) {saved = get_errno (); set_errno (what); }
     void set (int what) {set_errno (what); saved = what;}
     void reset () {saved = get_errno ();}
-    ~save_errno () {errno = _impure_ptr->_errno = saved;}
+    ~save_errno () {errno = _REENT_ERRNO(_impure_ptr) = saved;}
   };
 
 extern const char *__sp_fn;
diff --git a/winsup/cygwin/errno.cc b/winsup/cygwin/errno.cc
index f200e5b73..2f2c26984 100644
--- a/winsup/cygwin/errno.cc
+++ b/winsup/cygwin/errno.cc
@@ -335,7 +335,7 @@ void
 seterrno_from_win_error (const char *file, int line, DWORD code)
 {
   syscall_printf ("%s:%d windows error %u", file, line, code);
-  errno = _impure_ptr->_errno =  geterrno_from_win_error (code, EACCES);
+  errno = _REENT_ERRNO(_impure_ptr) =  geterrno_from_win_error (code, EACCES);
 }
 
 int
@@ -353,7 +353,7 @@ seterrno_from_nt_status (const char *file, int line, NTSTATUS status)
   SetLastError (code);
   syscall_printf ("%s:%d status %y -> windows error %u",
 		  file, line, status, code);
-  errno = _impure_ptr->_errno =  geterrno_from_win_error (code, EACCES);
+  errno = _REENT_ERRNO(_impure_ptr) =  geterrno_from_win_error (code, EACCES);
 }
 
 static char *
diff --git a/winsup/cygwin/strfuncs.cc b/winsup/cygwin/strfuncs.cc
index 9a97b3a24..22a79b614 100644
--- a/winsup/cygwin/strfuncs.cc
+++ b/winsup/cygwin/strfuncs.cc
@@ -144,7 +144,7 @@ __db_wctomb (struct _reent *r, char *s, wchar_t wchar, UINT cp)
   if (ret > 0 && !def_used)
     return ret;
 
-  r->_errno = EILSEQ;
+  _REENT_ERRNO(r) = EILSEQ;
   return -1;
 }
 
@@ -194,7 +194,7 @@ __eucjp_wctomb (struct _reent *r, char *s, wchar_t wchar, mbstate_t *state)
       return ret;
     }
 
-  r->_errno = EILSEQ;
+  _REENT_ERRNO(r) = EILSEQ;
   return -1;
 }
 
@@ -255,7 +255,7 @@ __db_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n, UINT cp,
 	 here is to check if the first byte returns a valid value... */
       else if (MultiByteToWideChar (cp, MB_ERR_INVALID_CHARS, s, 1, pwc, 1))
 	return 1;
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   state->__value.__wchb[state->__count] = *s;
@@ -263,7 +263,7 @@ __db_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n, UINT cp,
 			     (const char *) state->__value.__wchb, 2, pwc, 1);
   if (!ret)
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   state->__count = 0;
@@ -324,7 +324,7 @@ __eucjp_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
 	}
       else if (MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS, s, 1, pwc, 1))
 	return 1;
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   state->__value.__wchb[state->__count++] = *s;
@@ -347,7 +347,7 @@ jis_x_0212:
   if (!MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS,
 			    (const char *) state->__value.__wchb, 2, pwc, 1))
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   state->__count = 0;
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index 7d53c2af7..b8260b4d1 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -1900,7 +1900,7 @@ _fstat_r (struct _reent *ptr, int fd, struct stat *buf)
   int ret;
 
   if ((ret = fstat (fd, buf)) == -1)
-    ptr->_errno = get_errno ();
+    _REENT_ERRNO(ptr) = get_errno ();
   return ret;
 }
 
@@ -2051,7 +2051,7 @@ _stat_r (struct _reent *__restrict ptr, const char *__restrict name,
   int ret;
 
   if ((ret = stat (name, buf)) == -1)
-    ptr->_errno = get_errno ();
+    _REENT_ERRNO(ptr) = get_errno ();
   return ret;
 }
 
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 04/14] Add _REENT_STDIN(ptr)
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
                   ` (2 preceding siblings ...)
  2022-06-21 12:49 ` [PATCH 03/14] Add _REENT_ERRNO(ptr) Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 12:49 ` [PATCH 05/14] Add _REENT_STDOUT(ptr) Sebastian Huber
                   ` (10 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

Add a _REENT_STDIN() macro to encapsulate access to the _stdin
member of struct reent. This will help to replace the struct
member with a thread-local storage object in a follow up patch.
---
 newlib/libc/include/stdio.h     | 4 ++--
 newlib/libc/include/sys/reent.h | 1 +
 newlib/libc/include/wchar.h     | 4 ++--
 newlib/libc/machine/spu/stdio.c | 4 ++--
 newlib/libc/stdio/findfp.c      | 4 ++--
 5 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/newlib/libc/include/stdio.h b/newlib/libc/include/stdio.h
index b995b56d2..c802b3737 100644
--- a/newlib/libc/include/stdio.h
+++ b/newlib/libc/include/stdio.h
@@ -167,11 +167,11 @@ typedef _ssize_t ssize_t;
 
 #define	TMP_MAX		26
 
-#define	stdin	(_REENT->_stdin)
+#define	stdin	_REENT_STDIN(_REENT)
 #define	stdout	(_REENT->_stdout)
 #define	stderr	(_REENT->_stderr)
 
-#define _stdin_r(x)	((x)->_stdin)
+#define _stdin_r(x)	_REENT_STDIN(x)
 #define _stdout_r(x)	((x)->_stdout)
 #define _stderr_r(x)	((x)->_stderr)
 
diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 70a987100..801fe20f4 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -720,6 +720,7 @@ struct _reent
 
 #define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
 #define _REENT_ERRNO(_ptr)	((_ptr)->_errno)
+#define _REENT_STDIN(_ptr)	((_ptr)->_stdin)
 
 #define _REENT_INIT_PTR(var) \
   { memset((var), 0, sizeof(*(var))); \
diff --git a/newlib/libc/include/wchar.h b/newlib/libc/include/wchar.h
index 0d3e636f9..5e9202820 100644
--- a/newlib/libc/include/wchar.h
+++ b/newlib/libc/include/wchar.h
@@ -320,13 +320,13 @@ int	_wscanf_r (struct _reent *, const wchar_t *, ...);
 
 #define getwc(fp)	fgetwc(fp)
 #define putwc(wc,fp)	fputwc((wc), (fp))
-#define getwchar()	fgetwc(_REENT->_stdin)
+#define getwchar()	fgetwc(_REENT_STDIN(_REENT))
 #define putwchar(wc)	fputwc((wc), _REENT->_stdout)
 
 #if __GNU_VISIBLE
 #define getwc_unlocked(fp)	fgetwc_unlocked(fp)
 #define putwc_unlocked(wc,fp)	fputwc_unlocked((wc), (fp))
-#define getwchar_unlocked()	fgetwc_unlocked(_REENT->_stdin)
+#define getwchar_unlocked()	fgetwc_unlocked(_REENT_STDIN(_REENT))
 #define putwchar_unlocked(wc)	fputwc_unlocked((wc), _REENT->_stdout)
 #endif
 
diff --git a/newlib/libc/machine/spu/stdio.c b/newlib/libc/machine/spu/stdio.c
index 1dcc88b60..756628303 100644
--- a/newlib/libc/machine/spu/stdio.c
+++ b/newlib/libc/machine/spu/stdio.c
@@ -67,8 +67,8 @@ __sinit (struct _reent *s)
 {
   s->__cleanup = __cleanup;
 
-  s->_stdin = &s->__sf[0];
-  s->_stdin->_fp = SPE_STDIN;
+  _REENT_STDIN(s) = &s->__sf[0];
+  _REENT_STDIN(s)->_fp = SPE_STDIN;
 
   s->_stdout = &s->__sf[1];
   s->_stdout->_fp = SPE_STDOUT;
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index c7a4a941e..d2292d3e0 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -218,8 +218,8 @@ found:
 static void
 cleanup_stdio (struct _reent *ptr)
 {
-  if (ptr->_stdin != &__sf[0])
-    CLEANUP_FILE (ptr, ptr->_stdin);
+  if (_REENT_STDIN(ptr) != &__sf[0])
+    CLEANUP_FILE (ptr, _REENT_STDIN(ptr));
   if (ptr->_stdout != &__sf[1])
     CLEANUP_FILE (ptr, ptr->_stdout);
   if (ptr->_stderr != &__sf[2])
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 05/14] Add _REENT_STDOUT(ptr)
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
                   ` (3 preceding siblings ...)
  2022-06-21 12:49 ` [PATCH 04/14] Add _REENT_STDIN(ptr) Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 12:49 ` [PATCH 06/14] Add _REENT_STDERR(ptr) Sebastian Huber
                   ` (9 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

Add a _REENT_STDOUT() macro to encapsulate access to the _stdout
member of struct reent. This will help to replace the struct
member with a thread-local storage object in a follow up patch.
---
 newlib/libc/include/stdio.h     | 4 ++--
 newlib/libc/include/sys/reent.h | 1 +
 newlib/libc/include/wchar.h     | 4 ++--
 newlib/libc/machine/spu/stdio.c | 4 ++--
 newlib/libc/stdio/findfp.c      | 4 ++--
 5 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/newlib/libc/include/stdio.h b/newlib/libc/include/stdio.h
index c802b3737..95cc55fa9 100644
--- a/newlib/libc/include/stdio.h
+++ b/newlib/libc/include/stdio.h
@@ -168,11 +168,11 @@ typedef _ssize_t ssize_t;
 #define	TMP_MAX		26
 
 #define	stdin	_REENT_STDIN(_REENT)
-#define	stdout	(_REENT->_stdout)
+#define	stdout	_REENT_STDOUT(_REENT)
 #define	stderr	(_REENT->_stderr)
 
 #define _stdin_r(x)	_REENT_STDIN(x)
-#define _stdout_r(x)	((x)->_stdout)
+#define _stdout_r(x)	_REENT_STDOUT(x)
 #define _stderr_r(x)	((x)->_stderr)
 
 /*
diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 801fe20f4..652a330cb 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -721,6 +721,7 @@ struct _reent
 #define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
 #define _REENT_ERRNO(_ptr)	((_ptr)->_errno)
 #define _REENT_STDIN(_ptr)	((_ptr)->_stdin)
+#define _REENT_STDOUT(_ptr)	((_ptr)->_stdout)
 
 #define _REENT_INIT_PTR(var) \
   { memset((var), 0, sizeof(*(var))); \
diff --git a/newlib/libc/include/wchar.h b/newlib/libc/include/wchar.h
index 5e9202820..7c37427e6 100644
--- a/newlib/libc/include/wchar.h
+++ b/newlib/libc/include/wchar.h
@@ -321,13 +321,13 @@ int	_wscanf_r (struct _reent *, const wchar_t *, ...);
 #define getwc(fp)	fgetwc(fp)
 #define putwc(wc,fp)	fputwc((wc), (fp))
 #define getwchar()	fgetwc(_REENT_STDIN(_REENT))
-#define putwchar(wc)	fputwc((wc), _REENT->_stdout)
+#define putwchar(wc)	fputwc((wc), _REENT_STDOUT(_REENT))
 
 #if __GNU_VISIBLE
 #define getwc_unlocked(fp)	fgetwc_unlocked(fp)
 #define putwc_unlocked(wc,fp)	fputwc_unlocked((wc), (fp))
 #define getwchar_unlocked()	fgetwc_unlocked(_REENT_STDIN(_REENT))
-#define putwchar_unlocked(wc)	fputwc_unlocked((wc), _REENT->_stdout)
+#define putwchar_unlocked(wc)	fputwc_unlocked((wc), _REENT_STDOUT(_REENT))
 #endif
 
 _END_STD_C
diff --git a/newlib/libc/machine/spu/stdio.c b/newlib/libc/machine/spu/stdio.c
index 756628303..769b8f1c9 100644
--- a/newlib/libc/machine/spu/stdio.c
+++ b/newlib/libc/machine/spu/stdio.c
@@ -70,8 +70,8 @@ __sinit (struct _reent *s)
   _REENT_STDIN(s) = &s->__sf[0];
   _REENT_STDIN(s)->_fp = SPE_STDIN;
 
-  s->_stdout = &s->__sf[1];
-  s->_stdout->_fp = SPE_STDOUT;
+  _REENT_STDOUT(s) = &s->__sf[1];
+  _REENT_STDOUT(s)->_fp = SPE_STDOUT;
 
   s->_stderr = &s->__sf[2];
   s->_stderr->_fp = SPE_STDERR;
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index d2292d3e0..04e3517a7 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -220,8 +220,8 @@ cleanup_stdio (struct _reent *ptr)
 {
   if (_REENT_STDIN(ptr) != &__sf[0])
     CLEANUP_FILE (ptr, _REENT_STDIN(ptr));
-  if (ptr->_stdout != &__sf[1])
-    CLEANUP_FILE (ptr, ptr->_stdout);
+  if (_REENT_STDOUT(ptr) != &__sf[1])
+    CLEANUP_FILE (ptr, _REENT_STDOUT(ptr));
   if (ptr->_stderr != &__sf[2])
     CLEANUP_FILE (ptr, ptr->_stderr);
 }
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 06/14] Add _REENT_STDERR(ptr)
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
                   ` (4 preceding siblings ...)
  2022-06-21 12:49 ` [PATCH 05/14] Add _REENT_STDOUT(ptr) Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 12:49 ` [PATCH 07/14] Add _REENT_INC(ptr) Sebastian Huber
                   ` (8 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

Add a _REENT_STDERR() macro to encapsulate access to the _stderr
member of struct reent. This will help to replace the struct
member with a thread-local storage object in a follow up patch.
---
 newlib/libc/include/stdio.h     | 4 ++--
 newlib/libc/include/sys/reent.h | 1 +
 newlib/libc/machine/spu/stdio.c | 4 ++--
 newlib/libc/stdio/findfp.c      | 4 ++--
 4 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/newlib/libc/include/stdio.h b/newlib/libc/include/stdio.h
index 95cc55fa9..c75e48125 100644
--- a/newlib/libc/include/stdio.h
+++ b/newlib/libc/include/stdio.h
@@ -169,11 +169,11 @@ typedef _ssize_t ssize_t;
 
 #define	stdin	_REENT_STDIN(_REENT)
 #define	stdout	_REENT_STDOUT(_REENT)
-#define	stderr	(_REENT->_stderr)
+#define	stderr	_REENT_STDERR(_REENT)
 
 #define _stdin_r(x)	_REENT_STDIN(x)
 #define _stdout_r(x)	_REENT_STDOUT(x)
-#define _stderr_r(x)	((x)->_stderr)
+#define _stderr_r(x)	_REENT_STDERR(x)
 
 /*
  * Functions defined in ANSI C standard.
diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 652a330cb..c0647cfd9 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -722,6 +722,7 @@ struct _reent
 #define _REENT_ERRNO(_ptr)	((_ptr)->_errno)
 #define _REENT_STDIN(_ptr)	((_ptr)->_stdin)
 #define _REENT_STDOUT(_ptr)	((_ptr)->_stdout)
+#define _REENT_STDERR(_ptr)	((_ptr)->_stderr)
 
 #define _REENT_INIT_PTR(var) \
   { memset((var), 0, sizeof(*(var))); \
diff --git a/newlib/libc/machine/spu/stdio.c b/newlib/libc/machine/spu/stdio.c
index 769b8f1c9..83a60c06b 100644
--- a/newlib/libc/machine/spu/stdio.c
+++ b/newlib/libc/machine/spu/stdio.c
@@ -73,8 +73,8 @@ __sinit (struct _reent *s)
   _REENT_STDOUT(s) = &s->__sf[1];
   _REENT_STDOUT(s)->_fp = SPE_STDOUT;
 
-  s->_stderr = &s->__sf[2];
-  s->_stderr->_fp = SPE_STDERR;
+  _REENT_STDERR(s) = &s->__sf[2];
+  _REENT_STDERR(s)->_fp = SPE_STDERR;
 }
 
 void
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index 04e3517a7..3c888d03c 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -222,8 +222,8 @@ cleanup_stdio (struct _reent *ptr)
     CLEANUP_FILE (ptr, _REENT_STDIN(ptr));
   if (_REENT_STDOUT(ptr) != &__sf[1])
     CLEANUP_FILE (ptr, _REENT_STDOUT(ptr));
-  if (ptr->_stderr != &__sf[2])
-    CLEANUP_FILE (ptr, ptr->_stderr);
+  if (_REENT_STDERR(ptr) != &__sf[2])
+    CLEANUP_FILE (ptr, _REENT_STDERR(ptr));
 }
 
 /*
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 07/14] Add _REENT_INC(ptr)
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
                   ` (5 preceding siblings ...)
  2022-06-21 12:49 ` [PATCH 06/14] Add _REENT_STDERR(ptr) Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 12:49 ` [PATCH 08/14] Add _REENT_LOCALE(ptr) Sebastian Huber
                   ` (7 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

Add a _REENT_INC() macro to encapsulate access to the _inc member
of struct reent. This will help to replace the struct member with
a thread-local storage object in a follow up patch.
---
 newlib/libc/include/sys/reent.h | 1 +
 newlib/libc/stdio/tmpnam.c      | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index c0647cfd9..2c1593a5f 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -720,6 +720,7 @@ struct _reent
 
 #define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
 #define _REENT_ERRNO(_ptr)	((_ptr)->_errno)
+#define _REENT_INC(_ptr)	((_ptr)->_inc)
 #define _REENT_STDIN(_ptr)	((_ptr)->_stdin)
 #define _REENT_STDOUT(_ptr)	((_ptr)->_stdout)
 #define _REENT_STDERR(_ptr)	((_ptr)->_stderr)
diff --git a/newlib/libc/stdio/tmpnam.c b/newlib/libc/stdio/tmpnam.c
index 7379a7640..2767b4b20 100644
--- a/newlib/libc/stdio/tmpnam.c
+++ b/newlib/libc/stdio/tmpnam.c
@@ -135,9 +135,9 @@ _tmpnam_r (struct _reent *p,
     }
   pid = _getpid_r (p);
 
-  if (worker (p, result, P_tmpdir, "t", pid, &p->_inc))
+  if (worker (p, result, P_tmpdir, "t", pid, &_REENT_INC(p)))
     {
-      p->_inc++;
+      _REENT_INC(p)++;
       return result;
     }
 
@@ -162,7 +162,7 @@ _tempnam_r (struct _reent *p,
   if (filename)
     {
       if (! worker (p, filename, dir, prefix,
-		    _getpid_r (p) ^ (int) (_POINTER_INT) p, &p->_inc))
+		    _getpid_r (p) ^ (int) (_POINTER_INT) p, &_REENT_INC(p)))
 	return NULL;
     }
   return filename;
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 08/14] Add _REENT_LOCALE(ptr)
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
                   ` (6 preceding siblings ...)
  2022-06-21 12:49 ` [PATCH 07/14] Add _REENT_INC(ptr) Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 12:49 ` [PATCH 09/14] Add _REENT_CLEANUP(ptr) Sebastian Huber
                   ` (6 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

Add a _REENT_LOCALE() macro to encapsulate access to the _locale
member of struct reent. This will help to replace the struct
member with a thread-local storage object in a follow up patch.
---
 newlib/libc/include/sys/reent.h | 1 +
 newlib/libc/locale/setlocale.h  | 4 ++--
 newlib/libc/locale/uselocale.c  | 4 ++--
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 2c1593a5f..4296188f5 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -721,6 +721,7 @@ struct _reent
 #define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
 #define _REENT_ERRNO(_ptr)	((_ptr)->_errno)
 #define _REENT_INC(_ptr)	((_ptr)->_inc)
+#define _REENT_LOCALE(_ptr)	((_ptr)->_locale)
 #define _REENT_STDIN(_ptr)	((_ptr)->_stdin)
 #define _REENT_STDOUT(_ptr)	((_ptr)->_stdout)
 #define _REENT_STDERR(_ptr)	((_ptr)->_stderr)
diff --git a/newlib/libc/locale/setlocale.h b/newlib/libc/locale/setlocale.h
index a0c80843a..9f7fd7c10 100644
--- a/newlib/libc/locale/setlocale.h
+++ b/newlib/libc/locale/setlocale.h
@@ -218,7 +218,7 @@ _ELIDABLE_INLINE struct __locale_t *
 __get_locale_r (struct _reent *r)
 {
 #ifdef __HAVE_LOCALE_INFO__
-  return r->_locale;
+  return _REENT_LOCALE(r);
 #else
   return __get_global_locale();
 #endif
@@ -232,7 +232,7 @@ _ELIDABLE_INLINE struct __locale_t *
 __get_current_locale (void)
 {
 #ifdef __HAVE_LOCALE_INFO__
-  return _REENT->_locale ?: __get_global_locale ();
+  return _REENT_LOCALE(_REENT) ?: __get_global_locale ();
 #else
   return __get_global_locale();
 #endif
diff --git a/newlib/libc/locale/uselocale.c b/newlib/libc/locale/uselocale.c
index 83ebcdd19..799fb724a 100644
--- a/newlib/libc/locale/uselocale.c
+++ b/newlib/libc/locale/uselocale.c
@@ -64,9 +64,9 @@ _uselocale_r (struct _reent *p, struct __locale_t *newloc)
   if (!current_locale)
     current_locale = LC_GLOBAL_LOCALE;
   if (newloc == LC_GLOBAL_LOCALE)
-    p->_locale = NULL;
+    _REENT_LOCALE(p) = NULL;
   else if (newloc)
-    p->_locale = newloc;
+    _REENT_LOCALE(p) = newloc;
   return current_locale;
 }
 
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 09/14] Add _REENT_CLEANUP(ptr)
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
                   ` (7 preceding siblings ...)
  2022-06-21 12:49 ` [PATCH 08/14] Add _REENT_LOCALE(ptr) Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 12:49 ` [PATCH 10/14] Add _REENT_CVTLEN(ptr) Sebastian Huber
                   ` (5 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

Add a _REENT_CLEANUP() macro to encapsulate access to the
__cleanup member of struct reent. This will help to replace the
struct member with a thread-local storage object in a follow up
patch.
---
 newlib/libc/include/sys/reent.h  | 1 +
 newlib/libc/machine/spu/c99ppe.h | 2 +-
 newlib/libc/machine/spu/stdio.c  | 2 +-
 newlib/libc/reent/reent.c        | 4 ++--
 newlib/libc/stdio/findfp.c       | 4 ++--
 newlib/libc/stdio/local.h        | 2 +-
 newlib/libc/stdio/setvbuf.c      | 2 +-
 newlib/libc/sys/arm/syscalls.c   | 2 +-
 winsup/cygwin/cygtls.cc          | 2 +-
 winsup/cygwin/dcrt0.cc           | 2 +-
 10 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 4296188f5..3d277638f 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -718,6 +718,7 @@ struct _reent
 
 #endif /* !_REENT_SMALL */
 
+#define _REENT_CLEANUP(_ptr)	((_ptr)->__cleanup)
 #define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
 #define _REENT_ERRNO(_ptr)	((_ptr)->_errno)
 #define _REENT_INC(_ptr)	((_ptr)->_inc)
diff --git a/newlib/libc/machine/spu/c99ppe.h b/newlib/libc/machine/spu/c99ppe.h
index 263cf7fe0..7b3fcb6b7 100644
--- a/newlib/libc/machine/spu/c99ppe.h
+++ b/newlib/libc/machine/spu/c99ppe.h
@@ -104,6 +104,6 @@ FILE  *__sfp (struct _reent *);
 #define __sfp_free(fp) ( (fp)->_fp = 0 )
 
 #define CHECK_INIT(ptr) \
-  do { if ((ptr) && !(ptr)->__cleanup) __sinit (ptr); } while (0)
+  do { if ((ptr) && !_REENT_CLEANUP(ptr)) __sinit (ptr); } while (0)
 #define CHECK_STR_INIT(ptr) /* currently, do nothing */
 #endif /* __ASSEMBLER__ */
diff --git a/newlib/libc/machine/spu/stdio.c b/newlib/libc/machine/spu/stdio.c
index 83a60c06b..fd45b0014 100644
--- a/newlib/libc/machine/spu/stdio.c
+++ b/newlib/libc/machine/spu/stdio.c
@@ -65,7 +65,7 @@ __cleanup (struct _reent *s)
 void
 __sinit (struct _reent *s)
 {
-  s->__cleanup = __cleanup;
+  _REENT_CLEANUP(s) = __cleanup;
 
   _REENT_STDIN(s) = &s->__sf[0];
   _REENT_STDIN(s)->_fp = SPE_STDIN;
diff --git a/newlib/libc/reent/reent.c b/newlib/libc/reent/reent.c
index a9d44b455..16ac04e22 100644
--- a/newlib/libc/reent/reent.c
+++ b/newlib/libc/reent/reent.c
@@ -86,11 +86,11 @@ _reclaim_reent (struct _reent *ptr)
 	  if (ptr->_sig_func)
 	_free_r (ptr, ptr->_sig_func);*/
 
-      if (ptr->__cleanup)
+      if (_REENT_CLEANUP(ptr))
 	{
 	  /* cleanup won't reclaim memory 'coz usually it's run
 	     before the program exits, and who wants to wait for that? */
-	  ptr->__cleanup (ptr);
+	  _REENT_CLEANUP(ptr) (ptr);
 	}
 
       /* Malloc memory not reclaimed; no good way to return memory anyway. */
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index 3c888d03c..fbdb7f2f2 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -235,14 +235,14 @@ __sinit (struct _reent *s)
 {
   __sfp_lock_acquire ();
 
-  if (s->__cleanup)
+  if (_REENT_CLEANUP(s))
     {
       __sfp_lock_release ();
       return;
     }
 
   /* make sure we clean up on exit */
-  s->__cleanup = cleanup_stdio;	/* conservative */
+  _REENT_CLEANUP(s) = cleanup_stdio;	/* conservative */
 
   global_stdio_init ();
   __sfp_lock_release ();
diff --git a/newlib/libc/stdio/local.h b/newlib/libc/stdio/local.h
index 9b355e3ac..24eaff351 100644
--- a/newlib/libc/stdio/local.h
+++ b/newlib/libc/stdio/local.h
@@ -197,7 +197,7 @@ extern _READ_WRITE_RETURN_TYPE __swrite64 (struct _reent *, void *,
   do								\
     {								\
       struct _reent *_check_init_ptr = (ptr);			\
-      if ((_check_init_ptr) && !(_check_init_ptr)->__cleanup)	\
+      if ((_check_init_ptr) && !_REENT_CLEANUP(_check_init_ptr))\
 	__sinit (_check_init_ptr);				\
     }								\
   while (0)
diff --git a/newlib/libc/stdio/setvbuf.c b/newlib/libc/stdio/setvbuf.c
index 46c58a7b8..e27ea086c 100644
--- a/newlib/libc/stdio/setvbuf.c
+++ b/newlib/libc/stdio/setvbuf.c
@@ -174,7 +174,7 @@ nbf:
    * We're committed to buffering from here, so make sure we've
    * registered to flush buffers on exit.
    */
-  if (!reent->__cleanup)
+  if (!_REENT_CLEANUP(reent))
     __sinit(reent);
 
 #ifdef _FSEEK_OPTIMIZATION
diff --git a/newlib/libc/sys/arm/syscalls.c b/newlib/libc/sys/arm/syscalls.c
index 4b9be701a..325c0117f 100644
--- a/newlib/libc/sys/arm/syscalls.c
+++ b/newlib/libc/sys/arm/syscalls.c
@@ -60,7 +60,7 @@ extern void   __sinit (struct _reent *);
 #define CHECK_INIT(ptr) \
   do						\
     {						\
-      if ((ptr) && !(ptr)->__cleanup)		\
+      if ((ptr) && !_REENT_CLEANUP(ptr))	\
 	__sinit (ptr);				\
     }						\
   while (0)
diff --git a/winsup/cygwin/cygtls.cc b/winsup/cygwin/cygtls.cc
index 866342613..d0f4426c8 100644
--- a/winsup/cygwin/cygtls.cc
+++ b/winsup/cygwin/cygtls.cc
@@ -55,7 +55,7 @@ _cygtls::init_thread (void *x, DWORD (*func) (void *, void *))
       _REENT_INIT_PTR (&local_clib);
       stackptr = stack;
       altstack.ss_flags = SS_DISABLE;
-      if (_GLOBAL_REENT->__cleanup)
+      if (_REENT_CLEANUP(_GLOBAL_REENT))
 	local_clib.__cleanup = _cygtls::cleanup_early;
     }
 
diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc
index 9cbb8908f..897a2fba3 100644
--- a/winsup/cygwin/dcrt0.cc
+++ b/winsup/cygwin/dcrt0.cc
@@ -800,7 +800,7 @@ main_thread_sinit ()
      read or written in the first stdio function call in the main thread.
 
      To fix this issue we set __cleanup to _cygtls::cleanup_early here. */
-  _REENT->__cleanup = _cygtls::cleanup_early;
+  _REENT_CLEANUP(_REENT) = _cygtls::cleanup_early;
 }
 
 /* Take over from libc's crt0.o and start the application. Note the
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 10/14] Add _REENT_CVTLEN(ptr)
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
                   ` (8 preceding siblings ...)
  2022-06-21 12:49 ` [PATCH 09/14] Add _REENT_CLEANUP(ptr) Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 12:49 ` [PATCH 11/14] Add _REENT_CVTBUF(ptr) Sebastian Huber
                   ` (4 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

Add a _REENT_CVTLEN() macro to encapsulate access to the _cvtlen
member of struct reent. This will help to replace the struct
member with a thread-local storage object in a follow-up patch.
---
 newlib/libc/include/sys/reent.h | 1 +
 newlib/libc/stdlib/ecvtbuf.c    | 8 ++++----
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 3d277638f..8c88fae20 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -719,6 +719,7 @@ struct _reent
 #endif /* !_REENT_SMALL */
 
 #define _REENT_CLEANUP(_ptr)	((_ptr)->__cleanup)
+#define _REENT_CVTLEN(_ptr)	((_ptr)->_cvtlen)
 #define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
 #define _REENT_ERRNO(_ptr)	((_ptr)->_errno)
 #define _REENT_INC(_ptr)	((_ptr)->_inc)
diff --git a/newlib/libc/stdlib/ecvtbuf.c b/newlib/libc/stdlib/ecvtbuf.c
index 0cb11f889..0e776ab65 100644
--- a/newlib/libc/stdlib/ecvtbuf.c
+++ b/newlib/libc/stdlib/ecvtbuf.c
@@ -230,12 +230,12 @@ fcvtbuf (double invalue,
 
   if (fcvt_buf == NULL)
     {
-      if (reent->_cvtlen <= ndigit + 35)
+      if (_REENT_CVTLEN(reent) <= ndigit + 35)
 	{
 	  if ((fcvt_buf = (char *) _realloc_r (reent, reent->_cvtbuf,
 					       ndigit + 36)) == NULL)
 	    return NULL;
-	  reent->_cvtlen = ndigit + 36;
+	  _REENT_CVTLEN(reent) = ndigit + 36;
 	  reent->_cvtbuf = fcvt_buf;
 	}
 
@@ -279,12 +279,12 @@ ecvtbuf (double invalue,
 
   if (fcvt_buf == NULL)
     {
-      if (reent->_cvtlen <= ndigit)
+      if (_REENT_CVTLEN(reent) <= ndigit)
 	{
 	  if ((fcvt_buf = (char *) _realloc_r (reent, reent->_cvtbuf,
 					       ndigit + 1)) == NULL)
 	    return NULL;
-	  reent->_cvtlen = ndigit + 1;
+	  _REENT_CVTLEN(reent) = ndigit + 1;
 	  reent->_cvtbuf = fcvt_buf;
 	}
 
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 11/14] Add _REENT_CVTBUF(ptr)
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
                   ` (9 preceding siblings ...)
  2022-06-21 12:49 ` [PATCH 10/14] Add _REENT_CVTLEN(ptr) Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 12:49 ` [PATCH 12/14] Add _REENT_SIG_FUNC(ptr) Sebastian Huber
                   ` (3 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

Add a _REENT_CVTBUF() macro to encapsulate access to the _cvtbuf
member of struct reent. This will help to replace the struct
member with a thread-local storage object in a follow up patch.
---
 newlib/libc/include/sys/reent.h |  1 +
 newlib/libc/reent/reent.c       |  4 ++--
 newlib/libc/stdlib/ecvtbuf.c    | 12 ++++++------
 3 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 8c88fae20..ef05dd8c9 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -719,6 +719,7 @@ struct _reent
 #endif /* !_REENT_SMALL */
 
 #define _REENT_CLEANUP(_ptr)	((_ptr)->__cleanup)
+#define _REENT_CVTBUF(_ptr)	((_ptr)->_cvtbuf)
 #define _REENT_CVTLEN(_ptr)	((_ptr)->_cvtlen)
 #define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
 #define _REENT_ERRNO(_ptr)	((_ptr)->_errno)
diff --git a/newlib/libc/reent/reent.c b/newlib/libc/reent/reent.c
index 16ac04e22..2cfec5363 100644
--- a/newlib/libc/reent/reent.c
+++ b/newlib/libc/reent/reent.c
@@ -78,8 +78,8 @@ _reclaim_reent (struct _reent *ptr)
 	_free_r (ptr, ptr->_misc);
 #endif
 
-      if (ptr->_cvtbuf)
-	_free_r (ptr, ptr->_cvtbuf);
+      if (_REENT_CVTBUF(ptr))
+	_free_r (ptr, _REENT_CVTBUF(ptr));
     /* We should free _sig_func to avoid a memory leak, but how to
 	   do it safely considering that a signal may be delivered immediately
 	   after the free?
diff --git a/newlib/libc/stdlib/ecvtbuf.c b/newlib/libc/stdlib/ecvtbuf.c
index 0e776ab65..05f315108 100644
--- a/newlib/libc/stdlib/ecvtbuf.c
+++ b/newlib/libc/stdlib/ecvtbuf.c
@@ -232,14 +232,14 @@ fcvtbuf (double invalue,
     {
       if (_REENT_CVTLEN(reent) <= ndigit + 35)
 	{
-	  if ((fcvt_buf = (char *) _realloc_r (reent, reent->_cvtbuf,
+	  if ((fcvt_buf = (char *) _realloc_r (reent, _REENT_CVTBUF(reent),
 					       ndigit + 36)) == NULL)
 	    return NULL;
 	  _REENT_CVTLEN(reent) = ndigit + 36;
-	  reent->_cvtbuf = fcvt_buf;
+	  _REENT_CVTBUF(reent) = fcvt_buf;
 	}
 
-      fcvt_buf = reent->_cvtbuf ;
+      fcvt_buf = _REENT_CVTBUF(reent) ;
     }
 
   save = fcvt_buf;
@@ -281,14 +281,14 @@ ecvtbuf (double invalue,
     {
       if (_REENT_CVTLEN(reent) <= ndigit)
 	{
-	  if ((fcvt_buf = (char *) _realloc_r (reent, reent->_cvtbuf,
+	  if ((fcvt_buf = (char *) _realloc_r (reent, _REENT_CVTBUF(reent),
 					       ndigit + 1)) == NULL)
 	    return NULL;
 	  _REENT_CVTLEN(reent) = ndigit + 1;
-	  reent->_cvtbuf = fcvt_buf;
+	  _REENT_CVTBUF(reent) = fcvt_buf;
 	}
 
-      fcvt_buf = reent->_cvtbuf ;
+      fcvt_buf = _REENT_CVTBUF(reent) ;
     }
 
   save = fcvt_buf;
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 12/14] Add _REENT_SIG_FUNC(ptr)
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
                   ` (10 preceding siblings ...)
  2022-06-21 12:49 ` [PATCH 11/14] Add _REENT_CVTBUF(ptr) Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 12:49 ` [PATCH 13/14] Add _REENT_IS_NULL() Sebastian Huber
                   ` (2 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

Add a _REENT_SIG_FUNC() macro to encapsulate access to the
_sig_func member of struct reent. This will help to replace the
struct member with a thread-local storage object in a follow up
patch.
---
 newlib/libc/include/sys/reent.h |  1 +
 newlib/libc/reent/reent.c       |  4 ++--
 newlib/libc/signal/signal.c     | 26 +++++++++++++-------------
 3 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index ef05dd8c9..fa9c6cf82 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -725,6 +725,7 @@ struct _reent
 #define _REENT_ERRNO(_ptr)	((_ptr)->_errno)
 #define _REENT_INC(_ptr)	((_ptr)->_inc)
 #define _REENT_LOCALE(_ptr)	((_ptr)->_locale)
+#define _REENT_SIG_FUNC(_ptr)	((_ptr)->_sig_func)
 #define _REENT_STDIN(_ptr)	((_ptr)->_stdin)
 #define _REENT_STDOUT(_ptr)	((_ptr)->_stdout)
 #define _REENT_STDERR(_ptr)	((_ptr)->_stderr)
diff --git a/newlib/libc/reent/reent.c b/newlib/libc/reent/reent.c
index 2cfec5363..4e9485c7a 100644
--- a/newlib/libc/reent/reent.c
+++ b/newlib/libc/reent/reent.c
@@ -83,8 +83,8 @@ _reclaim_reent (struct _reent *ptr)
     /* We should free _sig_func to avoid a memory leak, but how to
 	   do it safely considering that a signal may be delivered immediately
 	   after the free?
-	  if (ptr->_sig_func)
-	_free_r (ptr, ptr->_sig_func);*/
+	  if (_REENT_SIG_FUNC(ptr))
+	_free_r (ptr, _REENT_SIG_FUNC(ptr));*/
 
       if (_REENT_CLEANUP(ptr))
 	{
diff --git a/newlib/libc/signal/signal.c b/newlib/libc/signal/signal.c
index a249e9f00..3f14b4712 100644
--- a/newlib/libc/signal/signal.c
+++ b/newlib/libc/signal/signal.c
@@ -93,14 +93,14 @@ _init_signal_r (struct _reent *ptr)
 {
   int i;
 
-  if (ptr->_sig_func == NULL)
+  if (_REENT_SIG_FUNC(ptr) == NULL)
     {
-      ptr->_sig_func = (_sig_func_ptr *)_malloc_r (ptr, sizeof (_sig_func_ptr) * NSIG);
-      if (ptr->_sig_func == NULL)
+      _REENT_SIG_FUNC(ptr) = (_sig_func_ptr *)_malloc_r (ptr, sizeof (_sig_func_ptr) * NSIG);
+      if (_REENT_SIG_FUNC(ptr) == NULL)
 	return -1;
 
       for (i = 0; i < NSIG; i++)
-	ptr->_sig_func[i] = SIG_DFL;
+	_REENT_SIG_FUNC(ptr)[i] = SIG_DFL;
     }
 
   return 0;
@@ -119,11 +119,11 @@ _signal_r (struct _reent *ptr,
       return SIG_ERR;
     }
 
-  if (ptr->_sig_func == NULL && _init_signal_r (ptr) != 0)
+  if (_REENT_SIG_FUNC(ptr) == NULL && _init_signal_r (ptr) != 0)
     return SIG_ERR;
   
-  old_func = ptr->_sig_func[sig];
-  ptr->_sig_func[sig] = func;
+  old_func = _REENT_SIG_FUNC(ptr)[sig];
+  _REENT_SIG_FUNC(ptr)[sig] = func;
 
   return old_func;
 }
@@ -140,10 +140,10 @@ _raise_r (struct _reent *ptr,
       return -1;
     }
 
-  if (ptr->_sig_func == NULL)
+  if (_REENT_SIG_FUNC(ptr) == NULL)
     func = SIG_DFL;
   else
-    func = ptr->_sig_func[sig];
+    func = _REENT_SIG_FUNC(ptr)[sig];
 
   if (func == SIG_DFL)
     return _kill_r (ptr, _getpid_r (ptr), sig);
@@ -156,7 +156,7 @@ _raise_r (struct _reent *ptr,
     }
   else
     {
-      ptr->_sig_func[sig] = SIG_DFL;
+      _REENT_SIG_FUNC(ptr)[sig] = SIG_DFL;
       func (sig);
       return 0;
     }
@@ -173,10 +173,10 @@ __sigtramp_r (struct _reent *ptr,
       return -1;
     }
 
-  if (ptr->_sig_func == NULL && _init_signal_r (ptr) != 0)
+  if (_REENT_SIG_FUNC(ptr) == NULL && _init_signal_r (ptr) != 0)
     return -1;
 
-  func = ptr->_sig_func[sig];
+  func = _REENT_SIG_FUNC(ptr)[sig];
   if (func == SIG_DFL)
     return 1;
   else if (func == SIG_ERR)
@@ -185,7 +185,7 @@ __sigtramp_r (struct _reent *ptr,
     return 3;
   else
     {
-      ptr->_sig_func[sig] = SIG_DFL;
+      _REENT_SIG_FUNC(ptr)[sig] = SIG_DFL;
       func (sig);
       return 0;
     }
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 13/14] Add _REENT_IS_NULL()
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
                   ` (11 preceding siblings ...)
  2022-06-21 12:49 ` [PATCH 12/14] Add _REENT_SIG_FUNC(ptr) Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 12:49 ` [PATCH 14/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
  2022-07-12 11:18 ` [PATCH 00/14] " Sebastian Huber
  14 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

In a follow up patch, struct _reent is optionally replaced by dedicated
thread-local objects.  In this case,_REENT is optionally defined to NULL.  Add
the _REENT_IS_NULL() macro to disable this check on demand.
---
 newlib/libc/include/sys/reent.h  | 2 ++
 newlib/libc/machine/spu/c99ppe.h | 8 +++++++-
 newlib/libc/stdio/local.h        | 3 ++-
 newlib/libc/sys/arm/syscalls.c   | 3 ++-
 4 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index fa9c6cf82..90f848180 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -763,6 +763,8 @@ extern struct _reent _impure_data __ATTRIBUTE_IMPURE_DATA__;
 # define _REENT _impure_ptr
 #endif /* __SINGLE_THREAD__ || !__DYNAMIC_REENT__ */
 
+#define _REENT_IS_NULL(_ptr) ((_ptr) == NULL)
+
 #define _GLOBAL_REENT (&_impure_data)
 
 /* This value is used in stdlib/misc.c.  reent/reent.c has to know it
diff --git a/newlib/libc/machine/spu/c99ppe.h b/newlib/libc/machine/spu/c99ppe.h
index 7b3fcb6b7..6e03d2cb9 100644
--- a/newlib/libc/machine/spu/c99ppe.h
+++ b/newlib/libc/machine/spu/c99ppe.h
@@ -104,6 +104,12 @@ FILE  *__sfp (struct _reent *);
 #define __sfp_free(fp) ( (fp)->_fp = 0 )
 
 #define CHECK_INIT(ptr) \
-  do { if ((ptr) && !_REENT_CLEANUP(ptr)) __sinit (ptr); } while (0)
+  do						\
+    {						\
+      if (!_REENT_IS_NULL(ptr) &&		\
+	  !_REENT_CLEANUP(ptr))			\
+	__sinit (ptr);				\
+    }						\
+  while (0)
 #define CHECK_STR_INIT(ptr) /* currently, do nothing */
 #endif /* __ASSEMBLER__ */
diff --git a/newlib/libc/stdio/local.h b/newlib/libc/stdio/local.h
index 24eaff351..b34c7c9d8 100644
--- a/newlib/libc/stdio/local.h
+++ b/newlib/libc/stdio/local.h
@@ -197,7 +197,8 @@ extern _READ_WRITE_RETURN_TYPE __swrite64 (struct _reent *, void *,
   do								\
     {								\
       struct _reent *_check_init_ptr = (ptr);			\
-      if ((_check_init_ptr) && !_REENT_CLEANUP(_check_init_ptr))\
+      if (!_REENT_IS_NULL(_check_init_ptr) &&			\
+	  !_REENT_CLEANUP(_check_init_ptr))			\
 	__sinit (_check_init_ptr);				\
     }								\
   while (0)
diff --git a/newlib/libc/sys/arm/syscalls.c b/newlib/libc/sys/arm/syscalls.c
index 325c0117f..dd49ca061 100644
--- a/newlib/libc/sys/arm/syscalls.c
+++ b/newlib/libc/sys/arm/syscalls.c
@@ -60,7 +60,8 @@ extern void   __sinit (struct _reent *);
 #define CHECK_INIT(ptr) \
   do						\
     {						\
-      if ((ptr) && !_REENT_CLEANUP(ptr))	\
+      if (!_REENT_IS_NULL(ptr) &&		\
+	  !_REENT_CLEANUP(ptr))			\
 	__sinit (ptr);				\
     }						\
   while (0)
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 14/14] Add --enable-newlib-reent-thread-local option
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
                   ` (12 preceding siblings ...)
  2022-06-21 12:49 ` [PATCH 13/14] Add _REENT_IS_NULL() Sebastian Huber
@ 2022-06-21 12:49 ` Sebastian Huber
  2022-06-21 13:59   ` Torbjorn SVENSSON
  2022-07-12 11:18 ` [PATCH 00/14] " Sebastian Huber
  14 siblings, 1 reply; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 12:49 UTC (permalink / raw)
  To: newlib

From: Matt Joyce <matthew.joyce@embedded-brains.de>

By default, Newlib uses a huge object of type struct _reent to store
thread-specific data.  This object is returned by __getreent() if the
__DYNAMIC_REENT__ Newlib configuration option is defined.

The reentrancy structure contains for example errno and the standard input,
output, and error file streams.  This means that if an application only uses
errno it has a dependency on the file stream support even if it does not use
it.  This is an issue for lower end targets and applications which need to
qualify the software according to safety standards (for example ECSS-E-ST-40C,
ECSS-Q-ST-80C, IEC 61508, ISO 26262, DO-178, DO-330, DO-333).

If the new _REENT_THREAD_LOCAL configuration option is enabled, then struct
_reent is replaced by dedicated thread-local objects for each struct _reent
member.  The thread-local objects are defined in translation units which use
the corresponding object.
---
 newlib/README                    |  8 +++
 newlib/configure                 | 21 ++++++++
 newlib/configure.ac              | 15 ++++++
 newlib/libc/errno/errno.c        |  6 +++
 newlib/libc/include/sys/config.h |  6 +++
 newlib/libc/include/sys/errno.h  |  6 +++
 newlib/libc/include/sys/reent.h  | 92 ++++++++++++++++++++++++++++++++
 newlib/libc/locale/locale.c      |  4 ++
 newlib/libc/reent/impure.c       |  4 ++
 newlib/libc/reent/reent.c        |  2 +
 newlib/libc/signal/signal.c      |  4 ++
 newlib/libc/stdio/findfp.c       |  7 +++
 newlib/libc/stdio/tmpnam.c       |  5 ++
 newlib/libc/stdlib/dtoa.c        |  5 ++
 newlib/libc/stdlib/ecvtbuf.c     |  5 ++
 newlib/libc/stdlib/l64a.c        |  4 ++
 newlib/libc/stdlib/lcong48.c     |  8 +++
 newlib/libc/stdlib/mblen.c       |  4 ++
 newlib/libc/stdlib/mbrlen.c      |  4 ++
 newlib/libc/stdlib/mbrtowc.c     |  4 ++
 newlib/libc/stdlib/mbsnrtowcs.c  |  4 ++
 newlib/libc/stdlib/mbtowc.c      |  4 ++
 newlib/libc/stdlib/mprec.c       |  5 ++
 newlib/libc/stdlib/rand.c        |  4 ++
 newlib/libc/stdlib/wcrtomb.c     |  4 ++
 newlib/libc/stdlib/wcsnrtombs.c  |  4 ++
 newlib/libc/stdlib/wctomb.c      |  4 ++
 newlib/libc/string/strsignal.c   |  4 ++
 newlib/libc/string/strtok.c      |  4 ++
 newlib/libc/time/asctime.c       |  4 ++
 newlib/libc/time/gmtime.c        |  4 ++
 newlib/libm/math/w_lgamma.c      |  4 ++
 newlib/newlib.hin                |  4 ++
 33 files changed, 267 insertions(+)

diff --git a/newlib/README b/newlib/README
index dd4686eeb..1b2c16cd4 100644
--- a/newlib/README
+++ b/newlib/README
@@ -304,6 +304,14 @@ One feature can be enabled by specifying `--enable-FEATURE=yes' or
      layout.
      Disabled by default.
 
+`--enable-newlib-reent-thread-local'
+     Enable thread-local storage objects as a replacement for struct _reent
+     members.  If enabled, then struct _reent is not defined and dedicated
+     thread-local storage objects are provided for each member of the default
+     struct _reent.  For statically linked executables only the objects
+     required by the application are linked in.
+     Disabled by default.
+
 `--disable-newlib-fvwrite-in-streamio'
      NEWLIB implements the vector buffer mechanism to support stream IO
      buffering required by C standard.  This feature is possibly
diff --git a/newlib/configure b/newlib/configure
index 82c256694..e493eb293 100755
--- a/newlib/configure
+++ b/newlib/configure
@@ -973,6 +973,7 @@ enable_newlib_atexit_dynamic_alloc
 enable_newlib_global_atexit
 enable_newlib_reent_small
 enable_newlib_reent_binary_compat
+enable_newlib_reent_thread_local
 enable_newlib_global_stdio_streams
 enable_newlib_fvwrite_in_streamio
 enable_newlib_fseek_optimization
@@ -1641,6 +1642,7 @@ Optional Features:
   --enable-newlib-global-atexit	enable atexit data structure as global
   --enable-newlib-reent-small   enable small reentrant struct support
   --enable-newlib-reent-binary-compat   enable backward binary compatibility for struct _reent
+  --enable-newlib-reent-thread-local   enable thread-local storage objects as a replacment for struct _reent members
   --enable-newlib-global-stdio-streams   enable global stdio streams
   --disable-newlib-fvwrite-in-streamio    disable iov in streamio
   --disable-newlib-fseek-optimization    disable fseek optimization
@@ -2404,6 +2406,19 @@ else
   newlib_reent_binary_compat=no
 fi
 
+# Check whether --enable-newlib-reent-thread-local was given.
+if test "${enable_newlib_reent_thread_local+set}" = set; then :
+  enableval=$enable_newlib_reent_thread_local; if test "${newlib_reent_thread_local+set}" != set; then
+  case "${enableval}" in
+    yes) newlib_reent_thread_local=yes ;;
+    no)  newlib_reent_thread_local=no  ;;
+    *)   as_fn_error $? "bad value ${enableval} for newlib-enable-reent-thread-local option" "$LINENO" 5 ;;
+  esac
+ fi
+else
+  newlib_reent_thread_local=no
+fi
+
 # Check whether --enable-newlib-global-stdio-streams was given.
 if test "${enable_newlib_global_stdio_streams+set}" = set; then :
   enableval=$enable_newlib_global_stdio_streams; case "${enableval}" in
@@ -6437,6 +6452,12 @@ $as_echo "#define _WANT_REENT_BACKWARD_BINARY_COMPAT 1" >>confdefs.h
 
 fi
 
+if test "${newlib_reent_thread_local}" = "yes"; then
+
+$as_echo "#define _WANT_REENT_THREAD_LOCAL 1" >>confdefs.h
+
+fi
+
 _mb_len_max=1
 if test "${newlib_mb}" = "yes"; then
 
diff --git a/newlib/configure.ac b/newlib/configure.ac
index 1951aab0c..bed444db0 100644
--- a/newlib/configure.ac
+++ b/newlib/configure.ac
@@ -173,6 +173,17 @@ AC_ARG_ENABLE(newlib-reent-binary-compat,
   esac
  fi], [newlib_reent_binary_compat=no])dnl
 
+dnl Support --enable-newlib-reent-thread-local
+AC_ARG_ENABLE(newlib-reent-thread-local,
+[  --enable-newlib-reent-thread-local   enable thread-local storage objects as a replacment for struct _reent members],
+[if test "${newlib_reent_thread_local+set}" != set; then
+  case "${enableval}" in
+    yes) newlib_reent_thread_local=yes ;;
+    no)  newlib_reent_thread_local=no  ;;
+    *)   AC_MSG_ERROR(bad value ${enableval} for newlib-enable-reent-thread-local option) ;;
+  esac
+ fi], [newlib_reent_thread_local=no])dnl
+
 dnl Support --enable-newlib-global-stdio-streams
 dnl This is no longer optional.  It is enabled in all Newlib configurations.
 AC_ARG_ENABLE(newlib-global-stdio-streams,
@@ -435,6 +446,10 @@ if test "${newlib_reent_binary_compat}" = "yes"; then
   AC_DEFINE(_WANT_REENT_BACKWARD_BINARY_COMPAT, 1, [Define to enable backward binary compatibility for struct _reent.])
 fi
 
+if test "${newlib_reent_thread_local}" = "yes"; then
+  AC_DEFINE(_WANT_REENT_THREAD_LOCAL, 1, [Define to enable thread-local storage objects as a replacment for struct _reent members.])
+fi
+
 _mb_len_max=1
 if test "${newlib_mb}" = "yes"; then
   AC_DEFINE(_MB_CAPABLE, 1, [Multibyte supported.])
diff --git a/newlib/libc/errno/errno.c b/newlib/libc/errno/errno.c
index 70402924b..384b07f2c 100644
--- a/newlib/libc/errno/errno.c
+++ b/newlib/libc/errno/errno.c
@@ -5,6 +5,10 @@
 #include <errno.h>
 #include <reent.h>
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local int _tls_errno;
+#else /* !_REENT_THREAD_LOCAL */
+
 #ifndef _REENT_ONLY
 
 int *
@@ -14,3 +18,5 @@ __errno ()
 }
 
 #endif
+
+#endif /* _REENT_THREAD_LOCAL */
diff --git a/newlib/libc/include/sys/config.h b/newlib/libc/include/sys/config.h
index 4bce10de3..5dcc77a80 100644
--- a/newlib/libc/include/sys/config.h
+++ b/newlib/libc/include/sys/config.h
@@ -297,6 +297,12 @@
 #endif
 #endif
 
+#ifdef _WANT_REENT_THREAD_LOCAL
+#ifndef _REENT_THREAD_LOCAL
+#define _REENT_THREAD_LOCAL
+#endif
+#endif
+
 /* If _MB_EXTENDED_CHARSETS_ALL is set, we want all of the extended
    charsets.  The extended charsets add a few functions and a couple
    of tables of a few K each. */
diff --git a/newlib/libc/include/sys/errno.h b/newlib/libc/include/sys/errno.h
index 995f30e05..f1509712e 100644
--- a/newlib/libc/include/sys/errno.h
+++ b/newlib/libc/include/sys/errno.h
@@ -10,11 +10,17 @@ extern "C" {
 
 #include <sys/reent.h>
 
+#ifdef _REENT_THREAD_LOCAL
+#define errno (_tls_errno)
+#else /* _REENT_THREAD_LOCAL */
+
 #ifndef _REENT_ONLY
 #define errno (*__errno())
 extern int *__errno (void);
 #endif
 
+#endif /* _REENT_THREAD_LOCAL */
+
 /* Please don't use these variables directly.
    Use strerror instead. */
 extern __IMPORT const char * const _sys_errlist[];
diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 90f848180..71342bebc 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -301,6 +301,8 @@ struct _rand48 {
 #define _REENT_ASCTIME_SIZE 26
 #define _REENT_SIGNAL_SIZE 24
 
+#ifndef _REENT_THREAD_LOCAL
+
 #ifdef _REENT_BACKWARD_BINARY_COMPAT
 #define _REENT_INIT_RESERVED_0 0,
 #define _REENT_INIT_RESERVED_1 0,
@@ -767,6 +769,96 @@ extern struct _reent _impure_data __ATTRIBUTE_IMPURE_DATA__;
 
 #define _GLOBAL_REENT (&_impure_data)
 
+#else /* _REENT_THREAD_LOCAL */
+
+#define _REENT NULL
+#define _GLOBAL_REENT NULL
+
+/*
+ * Since _REENT is defined as NULL, this macro ensures that calls to
+ * CHECK_INIT() do not automatically fail.
+ */
+#define _REENT_IS_NULL(_ptr) 0
+
+#define _REENT_CHECK_RAND48(ptr)	/* nothing */
+#define _REENT_CHECK_MP(ptr)		/* nothing */
+#define _REENT_CHECK_TM(ptr)		/* nothing */
+#define _REENT_CHECK_ASCTIME_BUF(ptr)	/* nothing */
+#define _REENT_CHECK_EMERGENCY(ptr)	/* nothing */
+#define _REENT_CHECK_MISC(ptr)		/* nothing */
+#define _REENT_CHECK_SIGNAL_BUF(ptr)	/* nothing */
+
+extern _Thread_local char _tls_asctime_buf[_REENT_ASCTIME_SIZE];
+#define _REENT_ASCTIME_BUF(_ptr) (_tls_asctime_buf)
+extern _Thread_local char *_tls_cvtbuf;
+#define _REENT_CVTBUF(_ptr) (_tls_cvtbuf)
+extern _Thread_local int _tls_cvtlen;
+#define _REENT_CVTLEN(_ptr) (_tls_cvtlen)
+extern _Thread_local void (*_tls_cleanup)(struct _reent *);
+#define _REENT_CLEANUP(_ptr) (_tls_cleanup)
+extern _Thread_local char _tls_emergency;
+#define _REENT_EMERGENCY(_ptr) (_tls_emergency)
+extern _Thread_local int _tls_errno;
+#define _REENT_ERRNO(_ptr) (_tls_errno)
+extern _Thread_local int _tls_getdate_err;
+#define _REENT_GETDATE_ERR_P(_ptr) (&_tls_getdate_err)
+extern _Thread_local int _tls_inc;
+#define _REENT_INC(_ptr) (_tls_inc)
+extern _Thread_local char _tls_l64a_buf[8];
+#define _REENT_L64A_BUF(_ptr) (_tls_l64a_buf)
+extern _Thread_local struct __locale_t *_tls_locale;
+#define _REENT_LOCALE(_ptr) (_tls_locale)
+extern _Thread_local _mbstate_t _tls_mblen_state;
+#define _REENT_MBLEN_STATE(_ptr) (_tls_mblen_state)
+extern _Thread_local _mbstate_t _tls_mbrlen_state;
+#define _REENT_MBRLEN_STATE(_ptr) (_tls_mbrlen_state)
+extern _Thread_local _mbstate_t _tls_mbrtowc_state;
+#define _REENT_MBRTOWC_STATE(_ptr) (_tls_mbrtowc_state)
+extern _Thread_local _mbstate_t _tls_mbsrtowcs_state;
+#define _REENT_MBSRTOWCS_STATE(_ptr) (_tls_mbsrtowcs_state)
+extern _Thread_local _mbstate_t _tls_mbtowc_state;
+#define _REENT_MBTOWC_STATE(_ptr) (_tls_mbtowc_state)
+extern _Thread_local struct _Bigint **_tls_mp_freelist;
+#define _REENT_MP_FREELIST(_ptr) (_tls_mp_freelist)
+extern _Thread_local struct _Bigint *_tls_mp_p5s;
+#define _REENT_MP_P5S(_ptr) (_tls_mp_p5s)
+extern _Thread_local struct _Bigint *_tls_mp_result;
+#define _REENT_MP_RESULT(_ptr) (_tls_mp_result)
+extern _Thread_local int _tls_mp_result_k;
+#define _REENT_MP_RESULT_K(_ptr) (_tls_mp_result_k)
+extern _Thread_local unsigned short _tls_rand48_add;
+#define _REENT_RAND48_ADD(_ptr) (_tls_rand48_add)
+extern _Thread_local unsigned short _tls_rand48_mult[3];
+#define _REENT_RAND48_MULT(_ptr) (_tls_rand48_mult)
+extern _Thread_local unsigned short _tls_rand48_seed[3];
+#define _REENT_RAND48_SEED(_ptr) (_tls_rand48_seed)
+extern _Thread_local unsigned long long _tls_rand_next;
+#define _REENT_RAND_NEXT(_ptr) (_tls_rand_next)
+extern _Thread_local void (**_tls_sig_func)(int);
+#define _REENT_SIG_FUNC(_ptr) (_tls_sig_func)
+extern _Thread_local char _tls_signal_buf[_REENT_SIGNAL_SIZE];
+#define _REENT_SIGNAL_BUF(_ptr) (_tls_signal_buf)
+extern _Thread_local int _tls_gamma_signgam;
+#define _REENT_SIGNGAM(_ptr) (_tls_gamma_signgam)
+extern _Thread_local __FILE *_tls_stdin;
+#define _REENT_STDIN(_ptr) (_tls_stdin)
+extern _Thread_local __FILE *_tls_stdout;
+#define _REENT_STDOUT(_ptr) (_tls_stdout)
+extern _Thread_local __FILE *_tls_stderr;
+#define _REENT_STDERR(_ptr) (_tls_stderr)
+extern _Thread_local char *_tls_strtok_last;
+#define _REENT_STRTOK_LAST(_ptr) (_tls_strtok_last)
+extern _Thread_local struct __tm _tls_localtime_buf;
+#define _REENT_TM(_ptr) (&_tls_localtime_buf)
+extern _Thread_local _mbstate_t _tls_wctomb_state;
+#define _REENT_WCTOMB_STATE(_ptr) (_tls_wctomb_state)
+extern _Thread_local _mbstate_t _tls_wcrtomb_state;
+#define _REENT_WCRTOMB_STATE(_ptr) (_tls_wcrtomb_state)
+extern _Thread_local _mbstate_t _tls_wcsrtombs_state;
+#define _REENT_WCSRTOMBS_STATE(_ptr) (_tls_wcsrtombs_state)
+
+#endif /* !_REENT_THREAD_LOCAL */
+
 /* This value is used in stdlib/misc.c.  reent/reent.c has to know it
    as well to make sure the freelist is correctly free'd.  Therefore
    we define it here, rather than in stdlib/misc.c, as before. */
diff --git a/newlib/libc/locale/locale.c b/newlib/libc/locale/locale.c
index 968642745..e523d2366 100644
--- a/newlib/libc/locale/locale.c
+++ b/newlib/libc/locale/locale.c
@@ -166,6 +166,10 @@ No supporting OS subroutines are required.
 #include "../ctype/ctype_.h"
 #include "../stdlib/local.h"
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local struct __locale_t *_tls_locale;
+#endif
+
 #ifdef __CYGWIN__ /* Has to be kept available as exported symbol for
 		     backward compatibility.  Set it in setlocale, but
 		     otherwise ignore it.  Applications compiled after
diff --git a/newlib/libc/reent/impure.c b/newlib/libc/reent/impure.c
index 9290c9cd5..a87b91feb 100644
--- a/newlib/libc/reent/impure.c
+++ b/newlib/libc/reent/impure.c
@@ -1,5 +1,7 @@
 #include <reent.h>
 
+#ifndef _REENT_THREAD_LOCAL
+
 /* Redeclare these symbols locally as weak so that the file containing
    their definitions (along with a lot of other stuff) isn't sucked in
    unless they are actually used by other compilation units.  This is
@@ -14,3 +16,5 @@ struct _reent __ATTRIBUTE_IMPURE_DATA__ _impure_data = _REENT_INIT (_impure_data
 extern struct _reent reent_data __attribute__ ((alias("_impure_data")));
 #endif
 struct _reent *__ATTRIBUTE_IMPURE_PTR__ _impure_ptr = &_impure_data;
+
+#endif /* _REENT_THREAD_LOCAL */
diff --git a/newlib/libc/reent/reent.c b/newlib/libc/reent/reent.c
index 4e9485c7a..db80ca06e 100644
--- a/newlib/libc/reent/reent.c
+++ b/newlib/libc/reent/reent.c
@@ -30,7 +30,9 @@ int errno;
 void
 _reclaim_reent (struct _reent *ptr)
 {
+#ifndef _REENT_THREAD_LOCAL
   if (ptr != _impure_ptr)
+#endif
     {
       /* used by mprec routines. */
 #ifdef _REENT_SMALL
diff --git a/newlib/libc/signal/signal.c b/newlib/libc/signal/signal.c
index 3f14b4712..77d78f04a 100644
--- a/newlib/libc/signal/signal.c
+++ b/newlib/libc/signal/signal.c
@@ -88,6 +88,10 @@ int _dummy_simulated_signal;
 #include <reent.h>
 #include <_syslist.h>
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local void (**_tls_sig_func)(int);
+#endif
+
 int
 _init_signal_r (struct _reent *ptr)
 {
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index fbdb7f2f2..c43028209 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -32,6 +32,13 @@ __FILE __sf[3];
 
 struct _glue __sglue = {NULL, 3, &__sf[0]};
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local __FILE *_tls_stdin = &__sf[0];
+_Thread_local __FILE *_tls_stdout = &__sf[1];
+_Thread_local __FILE *_tls_stderr = &__sf[2];
+_Thread_local void (*_tls_cleanup)(struct _reent *);
+#endif
+
 #ifdef _STDIO_BSD_SEMANTICS
   /* BSD and Glibc systems only flush streams which have been written to
      at exit time.  Calling flush rather than close for speed, as on
diff --git a/newlib/libc/stdio/tmpnam.c b/newlib/libc/stdio/tmpnam.c
index 2767b4b20..51dfd1cea 100644
--- a/newlib/libc/stdio/tmpnam.c
+++ b/newlib/libc/stdio/tmpnam.c
@@ -82,6 +82,11 @@ The global pointer <<environ>> is also required.
 #include <reent.h>
 #include <errno.h>
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local int _tls_inc;
+_Thread_local char _tls_emergency;
+#endif
+
 /* Try to open the file specified, if it can't be opened then try
    another one.  Return nonzero if successful, otherwise zero.  */
 
diff --git a/newlib/libc/stdlib/dtoa.c b/newlib/libc/stdlib/dtoa.c
index e47a8bc77..198fa663a 100644
--- a/newlib/libc/stdlib/dtoa.c
+++ b/newlib/libc/stdlib/dtoa.c
@@ -32,6 +32,11 @@
 #include <string.h>
 #include "mprec.h"
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local struct _Bigint *_tls_mp_result;
+_Thread_local int _tls_mp_result_k;
+#endif
+
 static int
 quorem (_Bigint * b, _Bigint * S)
 {
diff --git a/newlib/libc/stdlib/ecvtbuf.c b/newlib/libc/stdlib/ecvtbuf.c
index 05f315108..514896682 100644
--- a/newlib/libc/stdlib/ecvtbuf.c
+++ b/newlib/libc/stdlib/ecvtbuf.c
@@ -57,6 +57,11 @@ Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
 #include "mprec.h"
 #include "local.h"
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local char *_tls_cvtbuf;
+_Thread_local int _tls_cvtlen;
+#endif
+
 static void
 print_f (struct _reent *ptr,
 	char *buf,
diff --git a/newlib/libc/stdlib/l64a.c b/newlib/libc/stdlib/l64a.c
index 45282e32d..6f12b6151 100644
--- a/newlib/libc/stdlib/l64a.c
+++ b/newlib/libc/stdlib/l64a.c
@@ -24,6 +24,10 @@
 #include <stdlib.h>
 #include <reent.h>
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local char _tls_l64a_buf[8];
+#endif
+
 static const char R64_ARRAY[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
 char *
diff --git a/newlib/libc/stdlib/lcong48.c b/newlib/libc/stdlib/lcong48.c
index 78e9e5746..d1109f0ac 100644
--- a/newlib/libc/stdlib/lcong48.c
+++ b/newlib/libc/stdlib/lcong48.c
@@ -13,6 +13,14 @@
 
 #include "rand48.h"
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local unsigned short _tls_rand48_seed[3] = {_RAND48_SEED_0, _RAND48_SEED_1,
+    _RAND48_SEED_2};
+_Thread_local unsigned short _tls_rand48_mult[3] = {_RAND48_MULT_0, _RAND48_MULT_1,
+    _RAND48_MULT_2};
+_Thread_local unsigned short _tls_rand48_add = _RAND48_ADD;
+#endif
+
 void
 _lcong48_r (struct _reent *r,
        unsigned short p[7])
diff --git a/newlib/libc/stdlib/mblen.c b/newlib/libc/stdlib/mblen.c
index 3753d3673..24df61519 100644
--- a/newlib/libc/stdlib/mblen.c
+++ b/newlib/libc/stdlib/mblen.c
@@ -42,6 +42,10 @@ effects vary with the locale.
 #include <wchar.h>
 #include "local.h"
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local _mbstate_t _tls_mblen_state;
+#endif
+
 int
 mblen (const char *s,
         size_t n)
diff --git a/newlib/libc/stdlib/mbrlen.c b/newlib/libc/stdlib/mbrlen.c
index 57a733fed..39e175e43 100644
--- a/newlib/libc/stdlib/mbrlen.c
+++ b/newlib/libc/stdlib/mbrlen.c
@@ -5,6 +5,10 @@
 #include <stdio.h>
 #include <errno.h>
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local _mbstate_t _tls_mbrlen_state;
+#endif
+
 size_t
 mbrlen(const char *__restrict s, size_t n, mbstate_t *__restrict ps)
 {
diff --git a/newlib/libc/stdlib/mbrtowc.c b/newlib/libc/stdlib/mbrtowc.c
index 521b7a5f4..e641b8cd6 100644
--- a/newlib/libc/stdlib/mbrtowc.c
+++ b/newlib/libc/stdlib/mbrtowc.c
@@ -7,6 +7,10 @@
 #include <string.h>
 #include "local.h"
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local _mbstate_t _tls_mbrtowc_state;
+#endif
+
 size_t
 _mbrtowc_r (struct _reent *ptr,
 	wchar_t *pwc,
diff --git a/newlib/libc/stdlib/mbsnrtowcs.c b/newlib/libc/stdlib/mbsnrtowcs.c
index 8f94b1da5..2effc501c 100644
--- a/newlib/libc/stdlib/mbsnrtowcs.c
+++ b/newlib/libc/stdlib/mbsnrtowcs.c
@@ -70,6 +70,10 @@ PORTABILITY
 #include <stdio.h>
 #include <errno.h>
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local _mbstate_t _tls_mbsrtowcs_state;
+#endif
+
 size_t
 _mbsnrtowcs_r (struct _reent *r,
 	wchar_t *dst,
diff --git a/newlib/libc/stdlib/mbtowc.c b/newlib/libc/stdlib/mbtowc.c
index 2dc413f2d..fbd8df61d 100644
--- a/newlib/libc/stdlib/mbtowc.c
+++ b/newlib/libc/stdlib/mbtowc.c
@@ -49,6 +49,10 @@ effects vary with the locale.
 #include <wchar.h>
 #include "local.h"
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local _mbstate_t _tls_mbtowc_state;
+#endif
+
 int
 mbtowc (wchar_t *__restrict pwc,
         const char *__restrict s,
diff --git a/newlib/libc/stdlib/mprec.c b/newlib/libc/stdlib/mprec.c
index 930c984ca..1f534b068 100644
--- a/newlib/libc/stdlib/mprec.c
+++ b/newlib/libc/stdlib/mprec.c
@@ -86,6 +86,11 @@
 #include <reent.h>
 #include "mprec.h"
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local struct _Bigint *_tls_mp_p5s;
+_Thread_local struct _Bigint **_tls_mp_freelist;
+#endif
+
 /* This is defined in sys/reent.h as (sizeof (size_t) << 3) now, as in NetBSD.
    The old value of 15 was wrong and made newlib vulnerable against buffer
    overrun attacks (CVE-2009-0689), same as other implementations of gdtoa
diff --git a/newlib/libc/stdlib/rand.c b/newlib/libc/stdlib/rand.c
index 209cb32ff..ba9cc80f2 100644
--- a/newlib/libc/stdlib/rand.c
+++ b/newlib/libc/stdlib/rand.c
@@ -58,6 +58,10 @@ on two different systems.
 #include <stdlib.h>
 #include <reent.h>
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local unsigned long long _tls_rand_next = 1;
+#endif
+
 void
 srand (unsigned int seed)
 {
diff --git a/newlib/libc/stdlib/wcrtomb.c b/newlib/libc/stdlib/wcrtomb.c
index 6d670e23a..1b84720ac 100644
--- a/newlib/libc/stdlib/wcrtomb.c
+++ b/newlib/libc/stdlib/wcrtomb.c
@@ -6,6 +6,10 @@
 #include <errno.h>
 #include "local.h"
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local _mbstate_t _tls_wcrtomb_state;
+#endif
+
 size_t
 _wcrtomb_r (struct _reent *ptr,
 	char *s,
diff --git a/newlib/libc/stdlib/wcsnrtombs.c b/newlib/libc/stdlib/wcsnrtombs.c
index dfd974f24..abef7ac36 100644
--- a/newlib/libc/stdlib/wcsnrtombs.c
+++ b/newlib/libc/stdlib/wcsnrtombs.c
@@ -72,6 +72,10 @@ PORTABILITY
 #include "local.h"
 #include "../locale/setlocale.h"
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local _mbstate_t _tls_wcsrtombs_state;
+#endif
+
 size_t
 _wcsnrtombs_l (struct _reent *r, char *dst, const wchar_t **src, size_t nwc,
 	       size_t len, mbstate_t *ps, struct __locale_t *loc)
diff --git a/newlib/libc/stdlib/wctomb.c b/newlib/libc/stdlib/wctomb.c
index e908d22c2..f56dccf25 100644
--- a/newlib/libc/stdlib/wctomb.c
+++ b/newlib/libc/stdlib/wctomb.c
@@ -45,6 +45,10 @@ effects vary with the locale.
 #include <errno.h>
 #include "local.h"
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local _mbstate_t _tls_wctomb_state;
+#endif
+
 int
 wctomb (char *s,
         wchar_t wchar)
diff --git a/newlib/libc/string/strsignal.c b/newlib/libc/string/strsignal.c
index 544857b14..89d39fe3c 100644
--- a/newlib/libc/string/strsignal.c
+++ b/newlib/libc/string/strsignal.c
@@ -52,6 +52,10 @@ QUICKREF
 #include <stdlib.h>
 #include <reent.h>
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local char _tls_signal_buf[_REENT_SIGNAL_SIZE];
+#endif
+
 char *
 strsignal (int signal)
 {
diff --git a/newlib/libc/string/strtok.c b/newlib/libc/string/strtok.c
index 801f51aca..2a11c9196 100644
--- a/newlib/libc/string/strtok.c
+++ b/newlib/libc/string/strtok.c
@@ -74,6 +74,10 @@ QUICKREF
 #include <_ansi.h>
 #include <reent.h>
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local char *_tls_strtok_last;
+#endif
+
 #ifndef _REENT_ONLY
 
 extern char *__strtok_r (char *, const char *, char **, int);
diff --git a/newlib/libc/time/asctime.c b/newlib/libc/time/asctime.c
index 9aa26c3dc..a81506181 100644
--- a/newlib/libc/time/asctime.c
+++ b/newlib/libc/time/asctime.c
@@ -45,6 +45,10 @@ ANSI C requires <<asctime>>.
 #include <_ansi.h>
 #include <reent.h>
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local char _tls_asctime_buf[_REENT_ASCTIME_SIZE];
+#endif
+
 #ifndef _REENT_ONLY
 
 char *
diff --git a/newlib/libc/time/gmtime.c b/newlib/libc/time/gmtime.c
index 08e011129..a78d55433 100644
--- a/newlib/libc/time/gmtime.c
+++ b/newlib/libc/time/gmtime.c
@@ -47,6 +47,10 @@ ANSI C requires <<gmtime>>.
 
 #define _GMT_OFFSET 0
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local struct __tm _tls_localtime_buf;
+#endif
+
 #ifndef _REENT_ONLY
 
 struct tm *
diff --git a/newlib/libm/math/w_lgamma.c b/newlib/libm/math/w_lgamma.c
index c07804779..c075a4fc1 100644
--- a/newlib/libm/math/w_lgamma.c
+++ b/newlib/libm/math/w_lgamma.c
@@ -22,6 +22,10 @@
 #include <reent.h>
 #include <errno.h>
 
+#ifdef _REENT_THREAD_LOCAL
+_Thread_local int _tls_gamma_signgam;
+#endif
+
 #ifndef _DOUBLE_IS_32BITS
 
 #ifdef __STDC__
diff --git a/newlib/newlib.hin b/newlib/newlib.hin
index 30f59ddba..e87a5eabb 100644
--- a/newlib/newlib.hin
+++ b/newlib/newlib.hin
@@ -406,6 +406,10 @@
    restricted storage. */
 #undef _WANT_REENT_SMALL
 
+/* Define to enable thread-local storage objects as a replacment for struct
+   _reent members. */
+#undef _WANT_REENT_THREAD_LOCAL
+
 /* Register application finalization function using atexit. */
 #undef _WANT_REGISTER_FINI
 
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* RE: [PATCH 14/14] Add --enable-newlib-reent-thread-local option
  2022-06-21 12:49 ` [PATCH 14/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
@ 2022-06-21 13:59   ` Torbjorn SVENSSON
  2022-06-21 15:10     ` Sebastian Huber
  0 siblings, 1 reply; 30+ messages in thread
From: Torbjorn SVENSSON @ 2022-06-21 13:59 UTC (permalink / raw)
  To: Sebastian Huber, newlib

Hello Sebastian,

Just out of curiosity, how will this impact RTOSes like FreeRTOS?
Currently, FreeRTOS updates the impure_ptr to point to an instance of struct _reent that belongs to that particular task when the scheduler is switching task. 

See https://www.freertos.org/a00110.html#configUSE_NEWLIB_REENTRANT and https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c#L2047 for details.

Kind regards,
Torbjörn

> -----Original Message-----
> From: Newlib <newlib-
> bounces+torbjorn.svensson=st.com@sourceware.org> On Behalf Of
> Sebastian Huber
> Sent: den 21 juni 2022 14:50
> To: newlib@sourceware.org
> Subject: [PATCH 14/14] Add --enable-newlib-reent-thread-local option
> 
> From: Matt Joyce <matthew.joyce@embedded-brains.de>
> 
> By default, Newlib uses a huge object of type struct _reent to store
> thread-specific data.  This object is returned by __getreent() if the
> __DYNAMIC_REENT__ Newlib configuration option is defined.
> 
> The reentrancy structure contains for example errno and the standard input,
> output, and error file streams.  This means that if an application only uses
> errno it has a dependency on the file stream support even if it does not use
> it.  This is an issue for lower end targets and applications which need to
> qualify the software according to safety standards (for example ECSS-E-ST-
> 40C,
> ECSS-Q-ST-80C, IEC 61508, ISO 26262, DO-178, DO-330, DO-333).
> 
> If the new _REENT_THREAD_LOCAL configuration option is enabled, then
> struct
> _reent is replaced by dedicated thread-local objects for each struct _reent
> member.  The thread-local objects are defined in translation units which use
> the corresponding object.
> ---
>  newlib/README                    |  8 +++
>  newlib/configure                 | 21 ++++++++
>  newlib/configure.ac              | 15 ++++++
>  newlib/libc/errno/errno.c        |  6 +++
>  newlib/libc/include/sys/config.h |  6 +++
>  newlib/libc/include/sys/errno.h  |  6 +++
>  newlib/libc/include/sys/reent.h  | 92 ++++++++++++++++++++++++++++++++
>  newlib/libc/locale/locale.c      |  4 ++
>  newlib/libc/reent/impure.c       |  4 ++
>  newlib/libc/reent/reent.c        |  2 +
>  newlib/libc/signal/signal.c      |  4 ++
>  newlib/libc/stdio/findfp.c       |  7 +++
>  newlib/libc/stdio/tmpnam.c       |  5 ++
>  newlib/libc/stdlib/dtoa.c        |  5 ++
>  newlib/libc/stdlib/ecvtbuf.c     |  5 ++
>  newlib/libc/stdlib/l64a.c        |  4 ++
>  newlib/libc/stdlib/lcong48.c     |  8 +++
>  newlib/libc/stdlib/mblen.c       |  4 ++
>  newlib/libc/stdlib/mbrlen.c      |  4 ++
>  newlib/libc/stdlib/mbrtowc.c     |  4 ++
>  newlib/libc/stdlib/mbsnrtowcs.c  |  4 ++
>  newlib/libc/stdlib/mbtowc.c      |  4 ++
>  newlib/libc/stdlib/mprec.c       |  5 ++
>  newlib/libc/stdlib/rand.c        |  4 ++
>  newlib/libc/stdlib/wcrtomb.c     |  4 ++
>  newlib/libc/stdlib/wcsnrtombs.c  |  4 ++
>  newlib/libc/stdlib/wctomb.c      |  4 ++
>  newlib/libc/string/strsignal.c   |  4 ++
>  newlib/libc/string/strtok.c      |  4 ++
>  newlib/libc/time/asctime.c       |  4 ++
>  newlib/libc/time/gmtime.c        |  4 ++
>  newlib/libm/math/w_lgamma.c      |  4 ++
>  newlib/newlib.hin                |  4 ++
>  33 files changed, 267 insertions(+)
> 
> diff --git a/newlib/README b/newlib/README
> index dd4686eeb..1b2c16cd4 100644
> --- a/newlib/README
> +++ b/newlib/README
> @@ -304,6 +304,14 @@ One feature can be enabled by specifying `--enable-
> FEATURE=yes' or
>       layout.
>       Disabled by default.
> 
> +`--enable-newlib-reent-thread-local'
> +     Enable thread-local storage objects as a replacement for struct _reent
> +     members.  If enabled, then struct _reent is not defined and dedicated
> +     thread-local storage objects are provided for each member of the default
> +     struct _reent.  For statically linked executables only the objects
> +     required by the application are linked in.
> +     Disabled by default.
> +
>  `--disable-newlib-fvwrite-in-streamio'
>       NEWLIB implements the vector buffer mechanism to support stream IO
>       buffering required by C standard.  This feature is possibly
> diff --git a/newlib/configure b/newlib/configure
> index 82c256694..e493eb293 100755
> --- a/newlib/configure
> +++ b/newlib/configure
> @@ -973,6 +973,7 @@ enable_newlib_atexit_dynamic_alloc
>  enable_newlib_global_atexit
>  enable_newlib_reent_small
>  enable_newlib_reent_binary_compat
> +enable_newlib_reent_thread_local
>  enable_newlib_global_stdio_streams
>  enable_newlib_fvwrite_in_streamio
>  enable_newlib_fseek_optimization
> @@ -1641,6 +1642,7 @@ Optional Features:
>    --enable-newlib-global-atexit	enable atexit data structure as
> global
>    --enable-newlib-reent-small   enable small reentrant struct support
>    --enable-newlib-reent-binary-compat   enable backward binary
> compatibility for struct _reent
> +  --enable-newlib-reent-thread-local   enable thread-local storage objects as
> a replacment for struct _reent members
>    --enable-newlib-global-stdio-streams   enable global stdio streams
>    --disable-newlib-fvwrite-in-streamio    disable iov in streamio
>    --disable-newlib-fseek-optimization    disable fseek optimization
> @@ -2404,6 +2406,19 @@ else
>    newlib_reent_binary_compat=no
>  fi
> 
> +# Check whether --enable-newlib-reent-thread-local was given.
> +if test "${enable_newlib_reent_thread_local+set}" = set; then :
> +  enableval=$enable_newlib_reent_thread_local; if test
> "${newlib_reent_thread_local+set}" != set; then
> +  case "${enableval}" in
> +    yes) newlib_reent_thread_local=yes ;;
> +    no)  newlib_reent_thread_local=no  ;;
> +    *)   as_fn_error $? "bad value ${enableval} for newlib-enable-reent-
> thread-local option" "$LINENO" 5 ;;
> +  esac
> + fi
> +else
> +  newlib_reent_thread_local=no
> +fi
> +
>  # Check whether --enable-newlib-global-stdio-streams was given.
>  if test "${enable_newlib_global_stdio_streams+set}" = set; then :
>    enableval=$enable_newlib_global_stdio_streams; case "${enableval}" in
> @@ -6437,6 +6452,12 @@ $as_echo "#define
> _WANT_REENT_BACKWARD_BINARY_COMPAT 1" >>confdefs.h
> 
>  fi
> 
> +if test "${newlib_reent_thread_local}" = "yes"; then
> +
> +$as_echo "#define _WANT_REENT_THREAD_LOCAL 1" >>confdefs.h
> +
> +fi
> +
>  _mb_len_max=1
>  if test "${newlib_mb}" = "yes"; then
> 
> diff --git a/newlib/configure.ac b/newlib/configure.ac
> index 1951aab0c..bed444db0 100644
> --- a/newlib/configure.ac
> +++ b/newlib/configure.ac
> @@ -173,6 +173,17 @@ AC_ARG_ENABLE(newlib-reent-binary-compat,
>    esac
>   fi], [newlib_reent_binary_compat=no])dnl
> 
> +dnl Support --enable-newlib-reent-thread-local
> +AC_ARG_ENABLE(newlib-reent-thread-local,
> +[  --enable-newlib-reent-thread-local   enable thread-local storage objects as
> a replacment for struct _reent members],
> +[if test "${newlib_reent_thread_local+set}" != set; then
> +  case "${enableval}" in
> +    yes) newlib_reent_thread_local=yes ;;
> +    no)  newlib_reent_thread_local=no  ;;
> +    *)   AC_MSG_ERROR(bad value ${enableval} for newlib-enable-reent-
> thread-local option) ;;
> +  esac
> + fi], [newlib_reent_thread_local=no])dnl
> +
>  dnl Support --enable-newlib-global-stdio-streams
>  dnl This is no longer optional.  It is enabled in all Newlib configurations.
>  AC_ARG_ENABLE(newlib-global-stdio-streams,
> @@ -435,6 +446,10 @@ if test "${newlib_reent_binary_compat}" = "yes";
> then
>    AC_DEFINE(_WANT_REENT_BACKWARD_BINARY_COMPAT, 1, [Define to
> enable backward binary compatibility for struct _reent.])
>  fi
> 
> +if test "${newlib_reent_thread_local}" = "yes"; then
> +  AC_DEFINE(_WANT_REENT_THREAD_LOCAL, 1, [Define to enable thread-
> local storage objects as a replacment for struct _reent members.])
> +fi
> +
>  _mb_len_max=1
>  if test "${newlib_mb}" = "yes"; then
>    AC_DEFINE(_MB_CAPABLE, 1, [Multibyte supported.])
> diff --git a/newlib/libc/errno/errno.c b/newlib/libc/errno/errno.c
> index 70402924b..384b07f2c 100644
> --- a/newlib/libc/errno/errno.c
> +++ b/newlib/libc/errno/errno.c
> @@ -5,6 +5,10 @@
>  #include <errno.h>
>  #include <reent.h>
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local int _tls_errno;
> +#else /* !_REENT_THREAD_LOCAL */
> +
>  #ifndef _REENT_ONLY
> 
>  int *
> @@ -14,3 +18,5 @@ __errno ()
>  }
> 
>  #endif
> +
> +#endif /* _REENT_THREAD_LOCAL */
> diff --git a/newlib/libc/include/sys/config.h
> b/newlib/libc/include/sys/config.h
> index 4bce10de3..5dcc77a80 100644
> --- a/newlib/libc/include/sys/config.h
> +++ b/newlib/libc/include/sys/config.h
> @@ -297,6 +297,12 @@
>  #endif
>  #endif
> 
> +#ifdef _WANT_REENT_THREAD_LOCAL
> +#ifndef _REENT_THREAD_LOCAL
> +#define _REENT_THREAD_LOCAL
> +#endif
> +#endif
> +
>  /* If _MB_EXTENDED_CHARSETS_ALL is set, we want all of the extended
>     charsets.  The extended charsets add a few functions and a couple
>     of tables of a few K each. */
> diff --git a/newlib/libc/include/sys/errno.h b/newlib/libc/include/sys/errno.h
> index 995f30e05..f1509712e 100644
> --- a/newlib/libc/include/sys/errno.h
> +++ b/newlib/libc/include/sys/errno.h
> @@ -10,11 +10,17 @@ extern "C" {
> 
>  #include <sys/reent.h>
> 
> +#ifdef _REENT_THREAD_LOCAL
> +#define errno (_tls_errno)
> +#else /* _REENT_THREAD_LOCAL */
> +
>  #ifndef _REENT_ONLY
>  #define errno (*__errno())
>  extern int *__errno (void);
>  #endif
> 
> +#endif /* _REENT_THREAD_LOCAL */
> +
>  /* Please don't use these variables directly.
>     Use strerror instead. */
>  extern __IMPORT const char * const _sys_errlist[];
> diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
> index 90f848180..71342bebc 100644
> --- a/newlib/libc/include/sys/reent.h
> +++ b/newlib/libc/include/sys/reent.h
> @@ -301,6 +301,8 @@ struct _rand48 {
>  #define _REENT_ASCTIME_SIZE 26
>  #define _REENT_SIGNAL_SIZE 24
> 
> +#ifndef _REENT_THREAD_LOCAL
> +
>  #ifdef _REENT_BACKWARD_BINARY_COMPAT
>  #define _REENT_INIT_RESERVED_0 0,
>  #define _REENT_INIT_RESERVED_1 0,
> @@ -767,6 +769,96 @@ extern struct _reent _impure_data
> __ATTRIBUTE_IMPURE_DATA__;
> 
>  #define _GLOBAL_REENT (&_impure_data)
> 
> +#else /* _REENT_THREAD_LOCAL */
> +
> +#define _REENT NULL
> +#define _GLOBAL_REENT NULL
> +
> +/*
> + * Since _REENT is defined as NULL, this macro ensures that calls to
> + * CHECK_INIT() do not automatically fail.
> + */
> +#define _REENT_IS_NULL(_ptr) 0
> +
> +#define _REENT_CHECK_RAND48(ptr)	/* nothing */
> +#define _REENT_CHECK_MP(ptr)		/* nothing */
> +#define _REENT_CHECK_TM(ptr)		/* nothing */
> +#define _REENT_CHECK_ASCTIME_BUF(ptr)	/* nothing */
> +#define _REENT_CHECK_EMERGENCY(ptr)	/* nothing */
> +#define _REENT_CHECK_MISC(ptr)		/* nothing */
> +#define _REENT_CHECK_SIGNAL_BUF(ptr)	/* nothing */
> +
> +extern _Thread_local char _tls_asctime_buf[_REENT_ASCTIME_SIZE];
> +#define _REENT_ASCTIME_BUF(_ptr) (_tls_asctime_buf)
> +extern _Thread_local char *_tls_cvtbuf;
> +#define _REENT_CVTBUF(_ptr) (_tls_cvtbuf)
> +extern _Thread_local int _tls_cvtlen;
> +#define _REENT_CVTLEN(_ptr) (_tls_cvtlen)
> +extern _Thread_local void (*_tls_cleanup)(struct _reent *);
> +#define _REENT_CLEANUP(_ptr) (_tls_cleanup)
> +extern _Thread_local char _tls_emergency;
> +#define _REENT_EMERGENCY(_ptr) (_tls_emergency)
> +extern _Thread_local int _tls_errno;
> +#define _REENT_ERRNO(_ptr) (_tls_errno)
> +extern _Thread_local int _tls_getdate_err;
> +#define _REENT_GETDATE_ERR_P(_ptr) (&_tls_getdate_err)
> +extern _Thread_local int _tls_inc;
> +#define _REENT_INC(_ptr) (_tls_inc)
> +extern _Thread_local char _tls_l64a_buf[8];
> +#define _REENT_L64A_BUF(_ptr) (_tls_l64a_buf)
> +extern _Thread_local struct __locale_t *_tls_locale;
> +#define _REENT_LOCALE(_ptr) (_tls_locale)
> +extern _Thread_local _mbstate_t _tls_mblen_state;
> +#define _REENT_MBLEN_STATE(_ptr) (_tls_mblen_state)
> +extern _Thread_local _mbstate_t _tls_mbrlen_state;
> +#define _REENT_MBRLEN_STATE(_ptr) (_tls_mbrlen_state)
> +extern _Thread_local _mbstate_t _tls_mbrtowc_state;
> +#define _REENT_MBRTOWC_STATE(_ptr) (_tls_mbrtowc_state)
> +extern _Thread_local _mbstate_t _tls_mbsrtowcs_state;
> +#define _REENT_MBSRTOWCS_STATE(_ptr) (_tls_mbsrtowcs_state)
> +extern _Thread_local _mbstate_t _tls_mbtowc_state;
> +#define _REENT_MBTOWC_STATE(_ptr) (_tls_mbtowc_state)
> +extern _Thread_local struct _Bigint **_tls_mp_freelist;
> +#define _REENT_MP_FREELIST(_ptr) (_tls_mp_freelist)
> +extern _Thread_local struct _Bigint *_tls_mp_p5s;
> +#define _REENT_MP_P5S(_ptr) (_tls_mp_p5s)
> +extern _Thread_local struct _Bigint *_tls_mp_result;
> +#define _REENT_MP_RESULT(_ptr) (_tls_mp_result)
> +extern _Thread_local int _tls_mp_result_k;
> +#define _REENT_MP_RESULT_K(_ptr) (_tls_mp_result_k)
> +extern _Thread_local unsigned short _tls_rand48_add;
> +#define _REENT_RAND48_ADD(_ptr) (_tls_rand48_add)
> +extern _Thread_local unsigned short _tls_rand48_mult[3];
> +#define _REENT_RAND48_MULT(_ptr) (_tls_rand48_mult)
> +extern _Thread_local unsigned short _tls_rand48_seed[3];
> +#define _REENT_RAND48_SEED(_ptr) (_tls_rand48_seed)
> +extern _Thread_local unsigned long long _tls_rand_next;
> +#define _REENT_RAND_NEXT(_ptr) (_tls_rand_next)
> +extern _Thread_local void (**_tls_sig_func)(int);
> +#define _REENT_SIG_FUNC(_ptr) (_tls_sig_func)
> +extern _Thread_local char _tls_signal_buf[_REENT_SIGNAL_SIZE];
> +#define _REENT_SIGNAL_BUF(_ptr) (_tls_signal_buf)
> +extern _Thread_local int _tls_gamma_signgam;
> +#define _REENT_SIGNGAM(_ptr) (_tls_gamma_signgam)
> +extern _Thread_local __FILE *_tls_stdin;
> +#define _REENT_STDIN(_ptr) (_tls_stdin)
> +extern _Thread_local __FILE *_tls_stdout;
> +#define _REENT_STDOUT(_ptr) (_tls_stdout)
> +extern _Thread_local __FILE *_tls_stderr;
> +#define _REENT_STDERR(_ptr) (_tls_stderr)
> +extern _Thread_local char *_tls_strtok_last;
> +#define _REENT_STRTOK_LAST(_ptr) (_tls_strtok_last)
> +extern _Thread_local struct __tm _tls_localtime_buf;
> +#define _REENT_TM(_ptr) (&_tls_localtime_buf)
> +extern _Thread_local _mbstate_t _tls_wctomb_state;
> +#define _REENT_WCTOMB_STATE(_ptr) (_tls_wctomb_state)
> +extern _Thread_local _mbstate_t _tls_wcrtomb_state;
> +#define _REENT_WCRTOMB_STATE(_ptr) (_tls_wcrtomb_state)
> +extern _Thread_local _mbstate_t _tls_wcsrtombs_state;
> +#define _REENT_WCSRTOMBS_STATE(_ptr) (_tls_wcsrtombs_state)
> +
> +#endif /* !_REENT_THREAD_LOCAL */
> +
>  /* This value is used in stdlib/misc.c.  reent/reent.c has to know it
>     as well to make sure the freelist is correctly free'd.  Therefore
>     we define it here, rather than in stdlib/misc.c, as before. */
> diff --git a/newlib/libc/locale/locale.c b/newlib/libc/locale/locale.c
> index 968642745..e523d2366 100644
> --- a/newlib/libc/locale/locale.c
> +++ b/newlib/libc/locale/locale.c
> @@ -166,6 +166,10 @@ No supporting OS subroutines are required.
>  #include "../ctype/ctype_.h"
>  #include "../stdlib/local.h"
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local struct __locale_t *_tls_locale;
> +#endif
> +
>  #ifdef __CYGWIN__ /* Has to be kept available as exported symbol for
>  		     backward compatibility.  Set it in setlocale,
> but
>  		     otherwise ignore it.  Applications compiled
> after
> diff --git a/newlib/libc/reent/impure.c b/newlib/libc/reent/impure.c
> index 9290c9cd5..a87b91feb 100644
> --- a/newlib/libc/reent/impure.c
> +++ b/newlib/libc/reent/impure.c
> @@ -1,5 +1,7 @@
>  #include <reent.h>
> 
> +#ifndef _REENT_THREAD_LOCAL
> +
>  /* Redeclare these symbols locally as weak so that the file containing
>     their definitions (along with a lot of other stuff) isn't sucked in
>     unless they are actually used by other compilation units.  This is
> @@ -14,3 +16,5 @@ struct _reent __ATTRIBUTE_IMPURE_DATA__
> _impure_data = _REENT_INIT (_impure_data
>  extern struct _reent reent_data __attribute__ ((alias("_impure_data")));
>  #endif
>  struct _reent *__ATTRIBUTE_IMPURE_PTR__ _impure_ptr = &_impure_data;
> +
> +#endif /* _REENT_THREAD_LOCAL */
> diff --git a/newlib/libc/reent/reent.c b/newlib/libc/reent/reent.c
> index 4e9485c7a..db80ca06e 100644
> --- a/newlib/libc/reent/reent.c
> +++ b/newlib/libc/reent/reent.c
> @@ -30,7 +30,9 @@ int errno;
>  void
>  _reclaim_reent (struct _reent *ptr)
>  {
> +#ifndef _REENT_THREAD_LOCAL
>    if (ptr != _impure_ptr)
> +#endif
>      {
>        /* used by mprec routines. */
>  #ifdef _REENT_SMALL
> diff --git a/newlib/libc/signal/signal.c b/newlib/libc/signal/signal.c
> index 3f14b4712..77d78f04a 100644
> --- a/newlib/libc/signal/signal.c
> +++ b/newlib/libc/signal/signal.c
> @@ -88,6 +88,10 @@ int _dummy_simulated_signal;
>  #include <reent.h>
>  #include <_syslist.h>
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local void (**_tls_sig_func)(int);
> +#endif
> +
>  int
>  _init_signal_r (struct _reent *ptr)
>  {
> diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
> index fbdb7f2f2..c43028209 100644
> --- a/newlib/libc/stdio/findfp.c
> +++ b/newlib/libc/stdio/findfp.c
> @@ -32,6 +32,13 @@ __FILE __sf[3];
> 
>  struct _glue __sglue = {NULL, 3, &__sf[0]};
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local __FILE *_tls_stdin = &__sf[0];
> +_Thread_local __FILE *_tls_stdout = &__sf[1];
> +_Thread_local __FILE *_tls_stderr = &__sf[2];
> +_Thread_local void (*_tls_cleanup)(struct _reent *);
> +#endif
> +
>  #ifdef _STDIO_BSD_SEMANTICS
>    /* BSD and Glibc systems only flush streams which have been written to
>       at exit time.  Calling flush rather than close for speed, as on
> diff --git a/newlib/libc/stdio/tmpnam.c b/newlib/libc/stdio/tmpnam.c
> index 2767b4b20..51dfd1cea 100644
> --- a/newlib/libc/stdio/tmpnam.c
> +++ b/newlib/libc/stdio/tmpnam.c
> @@ -82,6 +82,11 @@ The global pointer <<environ>> is also required.
>  #include <reent.h>
>  #include <errno.h>
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local int _tls_inc;
> +_Thread_local char _tls_emergency;
> +#endif
> +
>  /* Try to open the file specified, if it can't be opened then try
>     another one.  Return nonzero if successful, otherwise zero.  */
> 
> diff --git a/newlib/libc/stdlib/dtoa.c b/newlib/libc/stdlib/dtoa.c
> index e47a8bc77..198fa663a 100644
> --- a/newlib/libc/stdlib/dtoa.c
> +++ b/newlib/libc/stdlib/dtoa.c
> @@ -32,6 +32,11 @@
>  #include <string.h>
>  #include "mprec.h"
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local struct _Bigint *_tls_mp_result;
> +_Thread_local int _tls_mp_result_k;
> +#endif
> +
>  static int
>  quorem (_Bigint * b, _Bigint * S)
>  {
> diff --git a/newlib/libc/stdlib/ecvtbuf.c b/newlib/libc/stdlib/ecvtbuf.c
> index 05f315108..514896682 100644
> --- a/newlib/libc/stdlib/ecvtbuf.c
> +++ b/newlib/libc/stdlib/ecvtbuf.c
> @@ -57,6 +57,11 @@ Supporting OS subroutines required: <<close>>,
> <<fstat>>, <<isatty>>,
>  #include "mprec.h"
>  #include "local.h"
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local char *_tls_cvtbuf;
> +_Thread_local int _tls_cvtlen;
> +#endif
> +
>  static void
>  print_f (struct _reent *ptr,
>  	char *buf,
> diff --git a/newlib/libc/stdlib/l64a.c b/newlib/libc/stdlib/l64a.c
> index 45282e32d..6f12b6151 100644
> --- a/newlib/libc/stdlib/l64a.c
> +++ b/newlib/libc/stdlib/l64a.c
> @@ -24,6 +24,10 @@
>  #include <stdlib.h>
>  #include <reent.h>
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local char _tls_l64a_buf[8];
> +#endif
> +
>  static const char R64_ARRAY[] =
> "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx
> yz";
> 
>  char *
> diff --git a/newlib/libc/stdlib/lcong48.c b/newlib/libc/stdlib/lcong48.c
> index 78e9e5746..d1109f0ac 100644
> --- a/newlib/libc/stdlib/lcong48.c
> +++ b/newlib/libc/stdlib/lcong48.c
> @@ -13,6 +13,14 @@
> 
>  #include "rand48.h"
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local unsigned short _tls_rand48_seed[3] = {_RAND48_SEED_0,
> _RAND48_SEED_1,
> +    _RAND48_SEED_2};
> +_Thread_local unsigned short _tls_rand48_mult[3] = {_RAND48_MULT_0,
> _RAND48_MULT_1,
> +    _RAND48_MULT_2};
> +_Thread_local unsigned short _tls_rand48_add = _RAND48_ADD;
> +#endif
> +
>  void
>  _lcong48_r (struct _reent *r,
>         unsigned short p[7])
> diff --git a/newlib/libc/stdlib/mblen.c b/newlib/libc/stdlib/mblen.c
> index 3753d3673..24df61519 100644
> --- a/newlib/libc/stdlib/mblen.c
> +++ b/newlib/libc/stdlib/mblen.c
> @@ -42,6 +42,10 @@ effects vary with the locale.
>  #include <wchar.h>
>  #include "local.h"
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local _mbstate_t _tls_mblen_state;
> +#endif
> +
>  int
>  mblen (const char *s,
>          size_t n)
> diff --git a/newlib/libc/stdlib/mbrlen.c b/newlib/libc/stdlib/mbrlen.c
> index 57a733fed..39e175e43 100644
> --- a/newlib/libc/stdlib/mbrlen.c
> +++ b/newlib/libc/stdlib/mbrlen.c
> @@ -5,6 +5,10 @@
>  #include <stdio.h>
>  #include <errno.h>
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local _mbstate_t _tls_mbrlen_state;
> +#endif
> +
>  size_t
>  mbrlen(const char *__restrict s, size_t n, mbstate_t *__restrict ps)
>  {
> diff --git a/newlib/libc/stdlib/mbrtowc.c b/newlib/libc/stdlib/mbrtowc.c
> index 521b7a5f4..e641b8cd6 100644
> --- a/newlib/libc/stdlib/mbrtowc.c
> +++ b/newlib/libc/stdlib/mbrtowc.c
> @@ -7,6 +7,10 @@
>  #include <string.h>
>  #include "local.h"
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local _mbstate_t _tls_mbrtowc_state;
> +#endif
> +
>  size_t
>  _mbrtowc_r (struct _reent *ptr,
>  	wchar_t *pwc,
> diff --git a/newlib/libc/stdlib/mbsnrtowcs.c
> b/newlib/libc/stdlib/mbsnrtowcs.c
> index 8f94b1da5..2effc501c 100644
> --- a/newlib/libc/stdlib/mbsnrtowcs.c
> +++ b/newlib/libc/stdlib/mbsnrtowcs.c
> @@ -70,6 +70,10 @@ PORTABILITY
>  #include <stdio.h>
>  #include <errno.h>
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local _mbstate_t _tls_mbsrtowcs_state;
> +#endif
> +
>  size_t
>  _mbsnrtowcs_r (struct _reent *r,
>  	wchar_t *dst,
> diff --git a/newlib/libc/stdlib/mbtowc.c b/newlib/libc/stdlib/mbtowc.c
> index 2dc413f2d..fbd8df61d 100644
> --- a/newlib/libc/stdlib/mbtowc.c
> +++ b/newlib/libc/stdlib/mbtowc.c
> @@ -49,6 +49,10 @@ effects vary with the locale.
>  #include <wchar.h>
>  #include "local.h"
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local _mbstate_t _tls_mbtowc_state;
> +#endif
> +
>  int
>  mbtowc (wchar_t *__restrict pwc,
>          const char *__restrict s,
> diff --git a/newlib/libc/stdlib/mprec.c b/newlib/libc/stdlib/mprec.c
> index 930c984ca..1f534b068 100644
> --- a/newlib/libc/stdlib/mprec.c
> +++ b/newlib/libc/stdlib/mprec.c
> @@ -86,6 +86,11 @@
>  #include <reent.h>
>  #include "mprec.h"
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local struct _Bigint *_tls_mp_p5s;
> +_Thread_local struct _Bigint **_tls_mp_freelist;
> +#endif
> +
>  /* This is defined in sys/reent.h as (sizeof (size_t) << 3) now, as in NetBSD.
>     The old value of 15 was wrong and made newlib vulnerable against buffer
>     overrun attacks (CVE-2009-0689), same as other implementations of gdtoa
> diff --git a/newlib/libc/stdlib/rand.c b/newlib/libc/stdlib/rand.c
> index 209cb32ff..ba9cc80f2 100644
> --- a/newlib/libc/stdlib/rand.c
> +++ b/newlib/libc/stdlib/rand.c
> @@ -58,6 +58,10 @@ on two different systems.
>  #include <stdlib.h>
>  #include <reent.h>
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local unsigned long long _tls_rand_next = 1;
> +#endif
> +
>  void
>  srand (unsigned int seed)
>  {
> diff --git a/newlib/libc/stdlib/wcrtomb.c b/newlib/libc/stdlib/wcrtomb.c
> index 6d670e23a..1b84720ac 100644
> --- a/newlib/libc/stdlib/wcrtomb.c
> +++ b/newlib/libc/stdlib/wcrtomb.c
> @@ -6,6 +6,10 @@
>  #include <errno.h>
>  #include "local.h"
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local _mbstate_t _tls_wcrtomb_state;
> +#endif
> +
>  size_t
>  _wcrtomb_r (struct _reent *ptr,
>  	char *s,
> diff --git a/newlib/libc/stdlib/wcsnrtombs.c
> b/newlib/libc/stdlib/wcsnrtombs.c
> index dfd974f24..abef7ac36 100644
> --- a/newlib/libc/stdlib/wcsnrtombs.c
> +++ b/newlib/libc/stdlib/wcsnrtombs.c
> @@ -72,6 +72,10 @@ PORTABILITY
>  #include "local.h"
>  #include "../locale/setlocale.h"
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local _mbstate_t _tls_wcsrtombs_state;
> +#endif
> +
>  size_t
>  _wcsnrtombs_l (struct _reent *r, char *dst, const wchar_t **src, size_t nwc,
>  	       size_t len, mbstate_t *ps, struct __locale_t *loc)
> diff --git a/newlib/libc/stdlib/wctomb.c b/newlib/libc/stdlib/wctomb.c
> index e908d22c2..f56dccf25 100644
> --- a/newlib/libc/stdlib/wctomb.c
> +++ b/newlib/libc/stdlib/wctomb.c
> @@ -45,6 +45,10 @@ effects vary with the locale.
>  #include <errno.h>
>  #include "local.h"
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local _mbstate_t _tls_wctomb_state;
> +#endif
> +
>  int
>  wctomb (char *s,
>          wchar_t wchar)
> diff --git a/newlib/libc/string/strsignal.c b/newlib/libc/string/strsignal.c
> index 544857b14..89d39fe3c 100644
> --- a/newlib/libc/string/strsignal.c
> +++ b/newlib/libc/string/strsignal.c
> @@ -52,6 +52,10 @@ QUICKREF
>  #include <stdlib.h>
>  #include <reent.h>
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local char _tls_signal_buf[_REENT_SIGNAL_SIZE];
> +#endif
> +
>  char *
>  strsignal (int signal)
>  {
> diff --git a/newlib/libc/string/strtok.c b/newlib/libc/string/strtok.c
> index 801f51aca..2a11c9196 100644
> --- a/newlib/libc/string/strtok.c
> +++ b/newlib/libc/string/strtok.c
> @@ -74,6 +74,10 @@ QUICKREF
>  #include <_ansi.h>
>  #include <reent.h>
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local char *_tls_strtok_last;
> +#endif
> +
>  #ifndef _REENT_ONLY
> 
>  extern char *__strtok_r (char *, const char *, char **, int);
> diff --git a/newlib/libc/time/asctime.c b/newlib/libc/time/asctime.c
> index 9aa26c3dc..a81506181 100644
> --- a/newlib/libc/time/asctime.c
> +++ b/newlib/libc/time/asctime.c
> @@ -45,6 +45,10 @@ ANSI C requires <<asctime>>.
>  #include <_ansi.h>
>  #include <reent.h>
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local char _tls_asctime_buf[_REENT_ASCTIME_SIZE];
> +#endif
> +
>  #ifndef _REENT_ONLY
> 
>  char *
> diff --git a/newlib/libc/time/gmtime.c b/newlib/libc/time/gmtime.c
> index 08e011129..a78d55433 100644
> --- a/newlib/libc/time/gmtime.c
> +++ b/newlib/libc/time/gmtime.c
> @@ -47,6 +47,10 @@ ANSI C requires <<gmtime>>.
> 
>  #define _GMT_OFFSET 0
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local struct __tm _tls_localtime_buf;
> +#endif
> +
>  #ifndef _REENT_ONLY
> 
>  struct tm *
> diff --git a/newlib/libm/math/w_lgamma.c b/newlib/libm/math/w_lgamma.c
> index c07804779..c075a4fc1 100644
> --- a/newlib/libm/math/w_lgamma.c
> +++ b/newlib/libm/math/w_lgamma.c
> @@ -22,6 +22,10 @@
>  #include <reent.h>
>  #include <errno.h>
> 
> +#ifdef _REENT_THREAD_LOCAL
> +_Thread_local int _tls_gamma_signgam;
> +#endif
> +
>  #ifndef _DOUBLE_IS_32BITS
> 
>  #ifdef __STDC__
> diff --git a/newlib/newlib.hin b/newlib/newlib.hin
> index 30f59ddba..e87a5eabb 100644
> --- a/newlib/newlib.hin
> +++ b/newlib/newlib.hin
> @@ -406,6 +406,10 @@
>     restricted storage. */
>  #undef _WANT_REENT_SMALL
> 
> +/* Define to enable thread-local storage objects as a replacment for struct
> +   _reent members. */
> +#undef _WANT_REENT_THREAD_LOCAL
> +
>  /* Register application finalization function using atexit. */
>  #undef _WANT_REGISTER_FINI
> 
> --
> 2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Fw: [PATCH 03/14] Add _REENT_ERRNO(ptr)
       [not found]   ` <BN2P110MB15447CD8C6DD815779AA7E4C9AB39@BN2P110MB1544.NAMP110.PROD.OUTLOOK.COM>
@ 2022-06-21 14:41     ` C Howland
  2022-06-23 10:55       ` Sebastian Huber
  0 siblings, 1 reply; 30+ messages in thread
From: C Howland @ 2022-06-21 14:41 UTC (permalink / raw)
  To: newlib

>
>
> ------------------------------
> *From:* Newlib <newlib-bounces+craig.howland=caci.com@sourceware.org> on
> behalf of Sebastian Huber <sebastian.huber@embedded-brains.de>
> *Sent:* Tuesday, June 21, 2022 8:49 AM
> *To:* newlib@sourceware.org <newlib@sourceware.org>
> *Subject:* [PATCH 03/14] Add _REENT_ERRNO(ptr)
>
>
>
> From: Matt Joyce <matthew.joyce@embedded-brains.de>
>
> Add a _REENT_ERRNO() macro to encapsulate the access to the
> _errno member of struct reent. This will help to replace the
> structure member with a thread-local storage object in a follow
> up patch.
> ---
>

There already exists an __errno_r() macro that does the very same function
(defined in sys/errno.h).  (Its use, however, is limited, only being used
in files under iconv/lib.)  Having the same thing done both ways probably
doesn't make sense.  The new name is more consistent with the rest of the
things being done, while the old name is established and errno is a more
specialized case.  It probably would be a good idea to either
1)  use __errno_r() instead of creating _REENT_ERRNO() or
2)  replace __errno_r() with _REENT_ERRNO() as part of adding the latter.
Craig

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH 14/14] Add --enable-newlib-reent-thread-local option
  2022-06-21 13:59   ` Torbjorn SVENSSON
@ 2022-06-21 15:10     ` Sebastian Huber
  0 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-21 15:10 UTC (permalink / raw)
  To: Torbjorn SVENSSON, newlib

Hello Torbjorn,

On 21/06/2022 15:59, Torbjorn SVENSSON wrote:
> Just out of curiosity, how will this impact RTOSes like FreeRTOS?

the new option is disabled by default. So, there will be no impact on 
existing systems (unless we made a mistake in the patch set).

> Currently, FreeRTOS updates the impure_ptr to point to an instance of struct _reent that belongs to that particular task when the scheduler is switching task.
> 
> Seehttps://www.freertos.org/a00110.html#configUSE_NEWLIB_REENTRANT  andhttps://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c#L2047  for details.

This will work as before. Using the new option for FreeRTOS could help 
to reduce the minimum storage foot print.

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Fw: [PATCH 03/14] Add _REENT_ERRNO(ptr)
  2022-06-21 14:41     ` Fw: " C Howland
@ 2022-06-23 10:55       ` Sebastian Huber
  2022-07-11  7:41         ` Sebastian Huber
  2022-07-12 16:11         ` Corinna Vinschen
  0 siblings, 2 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-06-23 10:55 UTC (permalink / raw)
  To: C Howland, newlib

On 21/06/2022 16:41, C Howland wrote:
>>
>> ------------------------------
>> *From:* Newlib<newlib-bounces+craig.howland=caci.com@sourceware.org>  on
>> behalf of Sebastian Huber<sebastian.huber@embedded-brains.de>
>> *Sent:* Tuesday, June 21, 2022 8:49 AM
>> *To:*newlib@sourceware.org  <newlib@sourceware.org>
>> *Subject:* [PATCH 03/14] Add _REENT_ERRNO(ptr)
>>
>>
>>
>> From: Matt Joyce<matthew.joyce@embedded-brains.de>
>>
>> Add a _REENT_ERRNO() macro to encapsulate the access to the
>> _errno member of struct reent. This will help to replace the
>> structure member with a thread-local storage object in a follow
>> up patch.
>> ---
>>
> There already exists an __errno_r() macro that does the very same function
> (defined in sys/errno.h).  (Its use, however, is limited, only being used
> in files under iconv/lib.)  Having the same thing done both ways probably
> doesn't make sense.  The new name is more consistent with the rest of the
> things being done, while the old name is established and errno is a more
> specialized case.  It probably would be a good idea to either
> 1)  use __errno_r() instead of creating _REENT_ERRNO() or
> 2)  replace __errno_r() with _REENT_ERRNO() as part of adding the latter.

I would not remove an existing macro, so option 1) would be preferred by me.

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Fw: [PATCH 03/14] Add _REENT_ERRNO(ptr)
  2022-06-23 10:55       ` Sebastian Huber
@ 2022-07-11  7:41         ` Sebastian Huber
  2022-07-12 16:11         ` Corinna Vinschen
  1 sibling, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-07-11  7:41 UTC (permalink / raw)
  To: C Howland, newlib

[-- Attachment #1: Type: text/plain, Size: 1913 bytes --]

On 23/06/2022 12:55, Sebastian Huber wrote:
> On 21/06/2022 16:41, C Howland wrote:
>>>
>>> ------------------------------
>>> *From:* Newlib<newlib-bounces+craig.howland=caci.com@sourceware.org>  on
>>> behalf of Sebastian Huber<sebastian.huber@embedded-brains.de>
>>> *Sent:* Tuesday, June 21, 2022 8:49 AM
>>> *To:*newlib@sourceware.org  <newlib@sourceware.org>
>>> *Subject:* [PATCH 03/14] Add _REENT_ERRNO(ptr)
>>>
>>>
>>>
>>> From: Matt Joyce<matthew.joyce@embedded-brains.de>
>>>
>>> Add a _REENT_ERRNO() macro to encapsulate the access to the
>>> _errno member of struct reent. This will help to replace the
>>> structure member with a thread-local storage object in a follow
>>> up patch.
>>> ---
>>>
>> There already exists an __errno_r() macro that does the very same 
>> function
>> (defined in sys/errno.h).  (Its use, however, is limited, only being used
>> in files under iconv/lib.)  Having the same thing done both ways probably
>> doesn't make sense.  The new name is more consistent with the rest of the
>> things being done, while the old name is established and errno is a more
>> specialized case.  It probably would be a good idea to either
>> 1)  use __errno_r() instead of creating _REENT_ERRNO() or
>> 2)  replace __errno_r() with _REENT_ERRNO() as part of adding the latter.
> 
> I would not remove an existing macro, so option 1) would be preferred by 
> me.

Attached is a patch which implements option 1).

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/

[-- Attachment #2: 0001-Use-__errno_r-ptr.patch --]
[-- Type: text/x-patch, Size: 78808 bytes --]

From 2a2c654eb37081932a8e50a993b98298deadd433 Mon Sep 17 00:00:00 2001
From: Matt Joyce <matthew.joyce@embedded-brains.de>
Date: Tue, 18 Jan 2022 10:13:04 +0100
Subject: [PATCH] Use __errno_r(ptr)

Use the __errno_r() macro to encapsulate the access to the _errno member of
struct reent.  This will help to replace the structure member with a
thread-local storage object in a follow up patch.

Move definition of __errno_r() to <sys/reent.h>.
---
 newlib/libc/ctype/wctrans.c               |  2 +-
 newlib/libc/ctype/wctype.c                |  2 +-
 newlib/libc/errno/errno.c                 |  2 +-
 newlib/libc/include/sys/errno.h           |  2 --
 newlib/libc/include/sys/reent.h           |  2 ++
 newlib/libc/machine/cris/sys/errno.h      |  2 --
 newlib/libc/machine/powerpc/strtosfix16.c | 10 +++----
 newlib/libc/machine/powerpc/strtosfix32.c | 10 +++----
 newlib/libc/machine/powerpc/strtosfix64.c | 10 +++----
 newlib/libc/machine/powerpc/strtoufix16.c | 10 +++----
 newlib/libc/machine/powerpc/strtoufix32.c | 10 +++----
 newlib/libc/machine/powerpc/strtoufix64.c | 10 +++----
 newlib/libc/machine/spu/stdio.c           |  2 +-
 newlib/libc/machine/spu/sys/errno.h       |  2 +-
 newlib/libc/reent/closer.c                |  2 +-
 newlib/libc/reent/execr.c                 |  6 ++--
 newlib/libc/reent/fcntlr.c                |  2 +-
 newlib/libc/reent/fstat64r.c              |  2 +-
 newlib/libc/reent/fstatr.c                |  2 +-
 newlib/libc/reent/gettimeofdayr.c         |  2 +-
 newlib/libc/reent/isattyr.c               |  2 +-
 newlib/libc/reent/linkr.c                 |  2 +-
 newlib/libc/reent/lseek64r.c              |  2 +-
 newlib/libc/reent/lseekr.c                |  2 +-
 newlib/libc/reent/mkdirr.c                |  2 +-
 newlib/libc/reent/open64r.c               |  2 +-
 newlib/libc/reent/openr.c                 |  2 +-
 newlib/libc/reent/readr.c                 |  2 +-
 newlib/libc/reent/renamer.c               |  2 +-
 newlib/libc/reent/sbrkr.c                 |  2 +-
 newlib/libc/reent/signalr.c               |  2 +-
 newlib/libc/reent/stat64r.c               |  2 +-
 newlib/libc/reent/statr.c                 |  2 +-
 newlib/libc/reent/unlinkr.c               |  2 +-
 newlib/libc/reent/writer.c                |  2 +-
 newlib/libc/signal/signal.c               |  6 ++--
 newlib/libc/stdio/asniprintf.c            |  4 +--
 newlib/libc/stdio/asnprintf.c             |  4 +--
 newlib/libc/stdio/fdopen.c                |  2 +-
 newlib/libc/stdio/fflush.c                | 18 ++++++------
 newlib/libc/stdio/fileno.c                |  2 +-
 newlib/libc/stdio/fileno_u.c              |  2 +-
 newlib/libc/stdio/findfp.c                |  2 +-
 newlib/libc/stdio/flags.c                 |  2 +-
 newlib/libc/stdio/fmemopen.c              | 14 +++++-----
 newlib/libc/stdio/fopencookie.c           | 14 +++++-----
 newlib/libc/stdio/fpurge.c                |  2 +-
 newlib/libc/stdio/freopen.c               |  4 +--
 newlib/libc/stdio/fseeko.c                |  4 +--
 newlib/libc/stdio/ftell.c                 |  2 +-
 newlib/libc/stdio/ftello.c                |  2 +-
 newlib/libc/stdio/funopen.c               | 16 +++++------
 newlib/libc/stdio/fvwrite.c               |  4 +--
 newlib/libc/stdio/mktemp.c                | 14 +++++-----
 newlib/libc/stdio/nano-vfprintf.c         | 10 +++----
 newlib/libc/stdio/open_memstream.c        | 14 +++++-----
 newlib/libc/stdio/perror.c                |  2 +-
 newlib/libc/stdio/refill.c                |  2 +-
 newlib/libc/stdio/sniprintf.c             |  8 +++---
 newlib/libc/stdio/snprintf.c              |  8 +++---
 newlib/libc/stdio/swprintf.c              |  8 +++---
 newlib/libc/stdio/tmpfile.c               |  6 ++--
 newlib/libc/stdio/tmpnam.c                |  2 +-
 newlib/libc/stdio/vasniprintf.c           |  2 +-
 newlib/libc/stdio/vasnprintf.c            |  2 +-
 newlib/libc/stdio/vfprintf.c              | 12 ++++----
 newlib/libc/stdio/vfscanf.c               |  2 +-
 newlib/libc/stdio/vfwprintf.c             |  4 +--
 newlib/libc/stdio/vfwscanf.c              |  2 +-
 newlib/libc/stdio/vsniprintf.c            |  4 +--
 newlib/libc/stdio/vsnprintf.c             |  4 +--
 newlib/libc/stdio/vswprintf.c             |  4 +--
 newlib/libc/stdio/wsetup.c                |  2 +-
 newlib/libc/stdio64/fdopen64.c            |  2 +-
 newlib/libc/stdio64/freopen64.c           |  4 +--
 newlib/libc/stdio64/fseeko64.c            |  6 ++--
 newlib/libc/stdio64/ftello64.c            |  2 +-
 newlib/libc/stdio64/tmpfile64.c           |  6 ++--
 newlib/libc/stdlib/__adjust.c             |  4 +--
 newlib/libc/stdlib/mbrtowc.c              |  4 +--
 newlib/libc/stdlib/mbsnrtowcs.c           |  2 +-
 newlib/libc/stdlib/mbtowc_r.c             | 34 +++++++++++------------
 newlib/libc/stdlib/strtod.c               | 10 +++----
 newlib/libc/stdlib/strtoimax.c            |  4 +--
 newlib/libc/stdlib/strtol.c               |  2 +-
 newlib/libc/stdlib/strtoll.c              |  2 +-
 newlib/libc/stdlib/strtoul.c              |  2 +-
 newlib/libc/stdlib/strtoull.c             |  2 +-
 newlib/libc/stdlib/strtoumax.c            |  4 +--
 newlib/libc/stdlib/wcrtomb.c              |  4 +--
 newlib/libc/stdlib/wcsnrtombs.c           |  2 +-
 newlib/libc/stdlib/wcstod.c               |  4 +--
 newlib/libc/stdlib/wcstoimax.c            |  4 +--
 newlib/libc/stdlib/wcstol.c               |  2 +-
 newlib/libc/stdlib/wcstoll.c              |  2 +-
 newlib/libc/stdlib/wcstoul.c              |  2 +-
 newlib/libc/stdlib/wcstoull.c             |  4 +--
 newlib/libc/stdlib/wcstoumax.c            |  4 +--
 newlib/libc/stdlib/wctomb_r.c             | 18 ++++++------
 newlib/libc/string/strerror.c             |  2 +-
 winsup/cygwin/cygerrno.h                  |  4 +--
 winsup/cygwin/errno.cc                    |  4 +--
 winsup/cygwin/strfuncs.cc                 | 12 ++++----
 winsup/cygwin/syscalls.cc                 |  4 +--
 104 files changed, 249 insertions(+), 251 deletions(-)

diff --git a/newlib/libc/ctype/wctrans.c b/newlib/libc/ctype/wctrans.c
index 4a58df152..1aa4e26bf 100644
--- a/newlib/libc/ctype/wctrans.c
+++ b/newlib/libc/ctype/wctrans.c
@@ -84,7 +84,7 @@ _wctrans_r (struct _reent *r,
     return WCT_TOUPPER;
   else
     {
-      r->_errno = EINVAL;
+      __errno_r(r) = EINVAL;
       return 0;
     }
 }
diff --git a/newlib/libc/ctype/wctype.c b/newlib/libc/ctype/wctype.c
index 5f2ae7a25..38a86ff5a 100644
--- a/newlib/libc/ctype/wctype.c
+++ b/newlib/libc/ctype/wctype.c
@@ -128,7 +128,7 @@ _wctype_r (struct _reent *r,
     }
 
   /* otherwise invalid */
-  r->_errno = EINVAL;
+  __errno_r(r) = EINVAL;
   return 0;
 }
 
diff --git a/newlib/libc/errno/errno.c b/newlib/libc/errno/errno.c
index fd1743d73..da24f883d 100644
--- a/newlib/libc/errno/errno.c
+++ b/newlib/libc/errno/errno.c
@@ -10,7 +10,7 @@
 int *
 __errno ()
 {
-  return &_REENT->_errno;
+  return &__errno_r(_REENT);
 }
 
 #endif
diff --git a/newlib/libc/include/sys/errno.h b/newlib/libc/include/sys/errno.h
index 025b461d4..8c93139b3 100644
--- a/newlib/libc/include/sys/errno.h
+++ b/newlib/libc/include/sys/errno.h
@@ -26,8 +26,6 @@ extern __IMPORT char *program_invocation_name;
 extern __IMPORT char *program_invocation_short_name;
 #endif
 
-#define __errno_r(ptr) ((ptr)->_errno)
-
 #define	EPERM 1		/* Not owner */
 #define	ENOENT 2	/* No such file or directory */
 #define	ESRCH 3		/* No such process */
diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 2b0ff46b7..ac5933842 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -718,6 +718,8 @@ struct _reent
 
 #endif /* !_REENT_SMALL */
 
+#define __errno_r(_ptr)		((_ptr)->_errno)
+
 #define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
 
 #define _REENT_INIT_PTR(var) \
diff --git a/newlib/libc/machine/cris/sys/errno.h b/newlib/libc/machine/cris/sys/errno.h
index 3d573015d..f259df82d 100644
--- a/newlib/libc/machine/cris/sys/errno.h
+++ b/newlib/libc/machine/cris/sys/errno.h
@@ -24,8 +24,6 @@ extern int *__errno (void);
 extern const char * const _sys_errlist[];
 extern int _sys_nerr;
 
-#define __errno_r(ptr) ((ptr)->_errno)
-
 /* Adjusted to the linux asm/errno.h */
 #define	EPERM		 1	/* Operation not permitted */
 #define	ENOENT		 2	/* No such file or directory */
diff --git a/newlib/libc/machine/powerpc/strtosfix16.c b/newlib/libc/machine/powerpc/strtosfix16.c
index 3fba45409..e15e8d558 100644
--- a/newlib/libc/machine/powerpc/strtosfix16.c
+++ b/newlib/libc/machine/powerpc/strtosfix16.c
@@ -108,10 +108,10 @@ _strtosfix16_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  __errno_r(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return SHRT_MIN;
       return SHRT_MAX;
@@ -120,12 +120,12 @@ _strtosfix16_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       return SHRT_MAX;
     }
   else if (dbl.d < -1.0)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       return SHRT_MIN;
     }
 
@@ -152,7 +152,7 @@ _strtosfix16_r (struct _reent *rptr,
       /* check if positive saturation has occurred because of rounding */
       if (!sign && result < 0)
 	{
-	  rptr->_errno = ERANGE;
+	  __errno_r(rptr) = ERANGE;
 	  return SHRT_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtosfix32.c b/newlib/libc/machine/powerpc/strtosfix32.c
index b21de9ea1..beb160860 100644
--- a/newlib/libc/machine/powerpc/strtosfix32.c
+++ b/newlib/libc/machine/powerpc/strtosfix32.c
@@ -29,10 +29,10 @@ _strtosfix32_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  __errno_r(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return LONG_MIN;
       return LONG_MAX;
@@ -41,12 +41,12 @@ _strtosfix32_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       return LONG_MAX;
     }
   else if (dbl.d < -1.0)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       return LONG_MIN;
     }
 
@@ -75,7 +75,7 @@ _strtosfix32_r (struct _reent *rptr,
       /* check if positive saturation has occurred because of rounding */
       if (!sign && result < 0)
 	{
-	  rptr->_errno = ERANGE;
+	  __errno_r(rptr) = ERANGE;
 	  return LONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtosfix64.c b/newlib/libc/machine/powerpc/strtosfix64.c
index 16032128f..d2f1f7ba9 100644
--- a/newlib/libc/machine/powerpc/strtosfix64.c
+++ b/newlib/libc/machine/powerpc/strtosfix64.c
@@ -32,10 +32,10 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (ld_type == 1)
 	{
-	  rptr->_errno = EDOM;
+	  __errno_r(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       if (word0(ldbl) & Sign_bit)
 	return LONG_LONG_MIN;
       return LONG_LONG_MAX;
@@ -63,7 +63,7 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (exp > 0 || (exp == 0 && tmp != 0x8000000000000000LL))
 	{
-	  rptr->_errno = ERANGE;
+	  __errno_r(rptr) = ERANGE;
 	  return LONG_LONG_MIN;
 	}
     }
@@ -71,7 +71,7 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (exp >= 0)
 	{
-	  rptr->_errno = ERANGE;
+	  __errno_r(rptr) = ERANGE;
 	  return LONG_LONG_MAX;
 	}
     }
@@ -88,7 +88,7 @@ _strtosfix64_r (struct _reent *rptr,
       /* check if positive saturation has occurred because of rounding */
       if (!sign && result < 0)
 	{
-	  rptr->_errno = ERANGE;
+	  __errno_r(rptr) = ERANGE;
 	  return LONG_LONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtoufix16.c b/newlib/libc/machine/powerpc/strtoufix16.c
index 9b16268bb..4c66a3fb4 100644
--- a/newlib/libc/machine/powerpc/strtoufix16.c
+++ b/newlib/libc/machine/powerpc/strtoufix16.c
@@ -106,10 +106,10 @@ _strtoufix16_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  __errno_r(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return 0;
       return USHRT_MAX;
@@ -118,12 +118,12 @@ _strtoufix16_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       return USHRT_MAX;
     }
   else if (dbl.d < 0)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       return 0;
     }
 
@@ -147,7 +147,7 @@ _strtoufix16_r (struct _reent *rptr,
       if (negexp == 0)
 	{
 	  /* we have overflow which means saturation */
-	  rptr->_errno = ERANGE;
+	  __errno_r(rptr) = ERANGE;
 	  return USHRT_MAX;
 	}
       result |= (1 << (16 - negexp));
diff --git a/newlib/libc/machine/powerpc/strtoufix32.c b/newlib/libc/machine/powerpc/strtoufix32.c
index 7d03f8c9b..02bad29f5 100644
--- a/newlib/libc/machine/powerpc/strtoufix32.c
+++ b/newlib/libc/machine/powerpc/strtoufix32.c
@@ -28,10 +28,10 @@ _strtoufix32_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  __errno_r(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return 0;
       return ULONG_MAX;
@@ -40,12 +40,12 @@ _strtoufix32_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       return ULONG_MAX;
     }
   else if (dbl.d < 0)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       return 0;
     }
 
@@ -77,7 +77,7 @@ _strtoufix32_r (struct _reent *rptr,
       /* if rounding causes carry, then saturation has occurred */
       if (result < tmp)
 	{
-	  rptr->_errno = ERANGE;
+	  __errno_r(rptr) = ERANGE;
 	  return ULONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtoufix64.c b/newlib/libc/machine/powerpc/strtoufix64.c
index a2f0484b3..8adfefa99 100644
--- a/newlib/libc/machine/powerpc/strtoufix64.c
+++ b/newlib/libc/machine/powerpc/strtoufix64.c
@@ -31,10 +31,10 @@ _strtoufix64_r (struct _reent *rptr,
     {
       if (ld_type == 1)
 	{
-	  rptr->_errno = EDOM;
+	  __errno_r(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       if (word0(ldbl) & Sign_bit)
 	return 0;
       return ULONG_LONG_MAX;
@@ -60,14 +60,14 @@ _strtoufix64_r (struct _reent *rptr,
   /* check for saturation */
   if (sign)
     {
-      rptr->_errno = ERANGE;
+      __errno_r(rptr) = ERANGE;
       return 0;
     }
   else
     {
       if (exp > 0 || (exp == 0 && tmp >= 0x8000000000000000LL))
 	{
-	  rptr->_errno = ERANGE;
+	  __errno_r(rptr) = ERANGE;
 	  return ULONG_LONG_MAX;
 	}
     }
@@ -89,7 +89,7 @@ _strtoufix64_r (struct _reent *rptr,
 	  /* if rounding causes carry, then saturation has occurred */
 	  if (result < tmp)
 	    {
-	      rptr->_errno = ERANGE;
+	      __errno_r(rptr) = ERANGE;
 	      return ULONG_LONG_MAX;
 	    }
 	}
diff --git a/newlib/libc/machine/spu/stdio.c b/newlib/libc/machine/spu/stdio.c
index 87e4c406d..825fa2d54 100644
--- a/newlib/libc/machine/spu/stdio.c
+++ b/newlib/libc/machine/spu/stdio.c
@@ -47,7 +47,7 @@ __sfp (struct _reent *d)
       return &__fp[i];
     }
   }
-  d->_errno = EMFILE;
+  __errno_r(d) = EMFILE;
   return NULL;
 }
 
diff --git a/newlib/libc/machine/spu/sys/errno.h b/newlib/libc/machine/spu/sys/errno.h
index b008ff6f4..754cf8198 100644
--- a/newlib/libc/machine/spu/sys/errno.h
+++ b/newlib/libc/machine/spu/sys/errno.h
@@ -34,7 +34,7 @@ extern "C" {
 extern const char * const _sys_errlist[];
 extern int _sys_nerr;
 
-#define __errno_r(ptr) ((ptr)->_errno)
+#define __errno_r(ptr) __errno_r(ptr)
 
 /* Adjusted to the linux asm/errno.h */
 #define	EPERM		 1	/* Operation not permitted */
diff --git a/newlib/libc/reent/closer.c b/newlib/libc/reent/closer.c
index deb34b002..1ac955844 100644
--- a/newlib/libc/reent/closer.c
+++ b/newlib/libc/reent/closer.c
@@ -45,7 +45,7 @@ _close_r (ptr, fd)
 
   errno = 0;
   if ((ret = _close (fd)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/execr.c b/newlib/libc/reent/execr.c
index 59b61223e..8c3c123fa 100644
--- a/newlib/libc/reent/execr.c
+++ b/newlib/libc/reent/execr.c
@@ -54,7 +54,7 @@ _execve_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _execve (name, argv, env)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
@@ -86,7 +86,7 @@ _fork_r (struct _reent *ptr)
 
   errno = 0;
   if ((ret = _fork ()) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
@@ -118,7 +118,7 @@ _wait_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _wait (status)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fcntlr.c b/newlib/libc/reent/fcntlr.c
index cd19d226f..e4a1bb67f 100644
--- a/newlib/libc/reent/fcntlr.c
+++ b/newlib/libc/reent/fcntlr.c
@@ -49,7 +49,7 @@ _fcntl_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _fcntl (fd, cmd, arg)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fstat64r.c b/newlib/libc/reent/fstat64r.c
index c546f5c1d..e14814fde 100644
--- a/newlib/libc/reent/fstat64r.c
+++ b/newlib/libc/reent/fstat64r.c
@@ -55,7 +55,7 @@ _fstat64_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _fstat64 (fd, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fstatr.c b/newlib/libc/reent/fstatr.c
index ec906c98d..6026a6377 100644
--- a/newlib/libc/reent/fstatr.c
+++ b/newlib/libc/reent/fstatr.c
@@ -53,7 +53,7 @@ _fstat_r (ptr, fd, pstat)
 
   errno = 0;
   if ((ret = _fstat (fd, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/gettimeofdayr.c b/newlib/libc/reent/gettimeofdayr.c
index 9b982a993..91c2a37c6 100644
--- a/newlib/libc/reent/gettimeofdayr.c
+++ b/newlib/libc/reent/gettimeofdayr.c
@@ -60,7 +60,7 @@ _gettimeofday_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _gettimeofday (ptimeval, ptimezone)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/isattyr.c b/newlib/libc/reent/isattyr.c
index f21bf25b2..01df8de85 100644
--- a/newlib/libc/reent/isattyr.c
+++ b/newlib/libc/reent/isattyr.c
@@ -50,7 +50,7 @@ _isatty_r (ptr, fd)
 
   errno = 0;
   if ((ret = _isatty (fd)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/linkr.c b/newlib/libc/reent/linkr.c
index b22da5f94..39eb17ba0 100644
--- a/newlib/libc/reent/linkr.c
+++ b/newlib/libc/reent/linkr.c
@@ -51,7 +51,7 @@ _link_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _link (old, new)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/lseek64r.c b/newlib/libc/reent/lseek64r.c
index 40769fb6d..d3fe02bbc 100644
--- a/newlib/libc/reent/lseek64r.c
+++ b/newlib/libc/reent/lseek64r.c
@@ -50,7 +50,7 @@ _lseek64_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _lseek64 (fd, pos, whence)) == (_off64_t) -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/lseekr.c b/newlib/libc/reent/lseekr.c
index ac2daaab9..142f028e7 100644
--- a/newlib/libc/reent/lseekr.c
+++ b/newlib/libc/reent/lseekr.c
@@ -47,7 +47,7 @@ _lseek_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _lseek (fd, pos, whence)) == (_off_t) -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/mkdirr.c b/newlib/libc/reent/mkdirr.c
index fd72df64c..054fa8797 100644
--- a/newlib/libc/reent/mkdirr.c
+++ b/newlib/libc/reent/mkdirr.c
@@ -48,7 +48,7 @@ _mkdir_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _mkdir (path, mode)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/open64r.c b/newlib/libc/reent/open64r.c
index 84bd67e34..0c02af187 100644
--- a/newlib/libc/reent/open64r.c
+++ b/newlib/libc/reent/open64r.c
@@ -52,7 +52,7 @@ _open64_r (ptr, file, flags, mode)
 
   errno = 0;
   if ((ret = _open64 (file, flags, mode)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/openr.c b/newlib/libc/reent/openr.c
index c6a7db5de..d22894cd3 100644
--- a/newlib/libc/reent/openr.c
+++ b/newlib/libc/reent/openr.c
@@ -48,7 +48,7 @@ _open_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _open (file, flags, mode)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/readr.c b/newlib/libc/reent/readr.c
index 7fccefd32..a06d0537a 100644
--- a/newlib/libc/reent/readr.c
+++ b/newlib/libc/reent/readr.c
@@ -47,7 +47,7 @@ _read_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = (_ssize_t)_read (fd, buf, cnt)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/renamer.c b/newlib/libc/reent/renamer.c
index 5420dc4a0..14cc2eb5b 100644
--- a/newlib/libc/reent/renamer.c
+++ b/newlib/libc/reent/renamer.c
@@ -49,7 +49,7 @@ _rename_r (struct _reent *ptr,
 #ifdef HAVE_RENAME
   errno = 0;
   if ((ret = _rename (old, new)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
 #else
   if (_link_r (ptr, old, new) == -1)
     return -1;
diff --git a/newlib/libc/reent/sbrkr.c b/newlib/libc/reent/sbrkr.c
index 21c4bd913..7b512e2e2 100644
--- a/newlib/libc/reent/sbrkr.c
+++ b/newlib/libc/reent/sbrkr.c
@@ -49,7 +49,7 @@ _sbrk_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = (char *)(_sbrk (incr))) == (void *) -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/signalr.c b/newlib/libc/reent/signalr.c
index 345910e4b..0e57b5d31 100644
--- a/newlib/libc/reent/signalr.c
+++ b/newlib/libc/reent/signalr.c
@@ -51,7 +51,7 @@ _kill_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _kill (pid, sig)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/stat64r.c b/newlib/libc/reent/stat64r.c
index b64736ef2..af7390db0 100644
--- a/newlib/libc/reent/stat64r.c
+++ b/newlib/libc/reent/stat64r.c
@@ -53,7 +53,7 @@ _stat64_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _stat64 (file, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/statr.c b/newlib/libc/reent/statr.c
index 9388e0246..efb1169c1 100644
--- a/newlib/libc/reent/statr.c
+++ b/newlib/libc/reent/statr.c
@@ -53,7 +53,7 @@ _stat_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _stat (file, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/unlinkr.c b/newlib/libc/reent/unlinkr.c
index 41bac0194..d0f68e58b 100644
--- a/newlib/libc/reent/unlinkr.c
+++ b/newlib/libc/reent/unlinkr.c
@@ -45,7 +45,7 @@ _unlink_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _unlink (file)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/writer.c b/newlib/libc/reent/writer.c
index 704aba18b..f08521ee1 100644
--- a/newlib/libc/reent/writer.c
+++ b/newlib/libc/reent/writer.c
@@ -47,7 +47,7 @@ _write_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = (_ssize_t)_write (fd, buf, cnt)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/signal/signal.c b/newlib/libc/signal/signal.c
index 0110287d8..fa4779d5a 100644
--- a/newlib/libc/signal/signal.c
+++ b/newlib/libc/signal/signal.c
@@ -115,7 +115,7 @@ _signal_r (struct _reent *ptr,
 
   if (sig < 0 || sig >= NSIG)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return SIG_ERR;
     }
 
@@ -136,7 +136,7 @@ _raise_r (struct _reent *ptr,
 
   if (sig < 0 || sig >= NSIG)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return -1;
     }
 
@@ -151,7 +151,7 @@ _raise_r (struct _reent *ptr,
     return 0;
   else if (func == SIG_ERR)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return 1;
     }
   else
diff --git a/newlib/libc/stdio/asniprintf.c b/newlib/libc/stdio/asniprintf.c
index 0bfe00d9b..17e232d10 100644
--- a/newlib/libc/stdio/asniprintf.c
+++ b/newlib/libc/stdio/asniprintf.c
@@ -42,7 +42,7 @@ _asniprintf_r (struct _reent *ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
@@ -88,7 +88,7 @@ asniprintf (char *buf,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/asnprintf.c b/newlib/libc/stdio/asnprintf.c
index f657f9ec9..2397ce40e 100644
--- a/newlib/libc/stdio/asnprintf.c
+++ b/newlib/libc/stdio/asnprintf.c
@@ -42,7 +42,7 @@ _asnprintf_r (struct _reent *__restrict ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
@@ -94,7 +94,7 @@ asnprintf (char *__restrict buf,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/fdopen.c b/newlib/libc/stdio/fdopen.c
index ef942c284..52c0cc2bc 100644
--- a/newlib/libc/stdio/fdopen.c
+++ b/newlib/libc/stdio/fdopen.c
@@ -73,7 +73,7 @@ _fdopen_r (struct _reent *ptr,
   fdmode = fdflags & O_ACCMODE;
   if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
     {
-      ptr->_errno = EBADF;
+      __errno_r(ptr) = EBADF;
       return 0;
     }
 #endif
diff --git a/newlib/libc/stdio/fflush.c b/newlib/libc/stdio/fflush.c
index bbec4a19b..170ad4cd8 100644
--- a/newlib/libc/stdio/fflush.c
+++ b/newlib/libc/stdio/fflush.c
@@ -137,8 +137,8 @@ __sflush_r (struct _reent *ptr,
 	  /* Save last errno and set errno to 0, so we can check if a device
 	     returns with a valid position -1.  We restore the last errno if
 	     no other error condition has been encountered. */
-	  tmp_errno = ptr->_errno;
-	  ptr->_errno = 0;
+	  tmp_errno = __errno_r(ptr);
+	  __errno_r(ptr) = 0;
 	  /* Get the physical position we are at in the file.  */
 	  if (fp->_flags & __SOFF)
 	    curoff = fp->_offset;
@@ -152,13 +152,13 @@ __sflush_r (struct _reent *ptr,
 	      else
 #endif
 		curoff = fp->_seek (ptr, fp->_cookie, 0, SEEK_CUR);
-	      if (curoff == -1L && ptr->_errno != 0)
+	      if (curoff == -1L && __errno_r(ptr) != 0)
 		{
 		  int result = EOF;
-		  if (ptr->_errno == ESPIPE || ptr->_errno == EINVAL)
+		  if (__errno_r(ptr) == ESPIPE || __errno_r(ptr) == EINVAL)
 		    {
 		      result = 0;
-		      ptr->_errno = tmp_errno;
+		      __errno_r(ptr) = tmp_errno;
 		    }
 		  else
 		    fp->_flags |= __SERR;
@@ -180,8 +180,8 @@ __sflush_r (struct _reent *ptr,
 	  else
 #endif
 	    curoff = fp->_seek (ptr, fp->_cookie, curoff, SEEK_SET);
-	  if (curoff != -1 || ptr->_errno == 0
-	      || ptr->_errno == ESPIPE || ptr->_errno == EINVAL)
+	  if (curoff != -1 || __errno_r(ptr) == 0
+	      || __errno_r(ptr) == ESPIPE || __errno_r(ptr) == EINVAL)
 	    {
 	      /* Seek successful or ignorable error condition.
 		 We can clear read buffer now.  */
@@ -190,9 +190,9 @@ __sflush_r (struct _reent *ptr,
 #endif
 	      fp->_r = 0;
 	      fp->_p = fp->_bf._base;
-	      if ((fp->_flags & __SOFF) && (curoff != -1 || ptr->_errno == 0))
+	      if ((fp->_flags & __SOFF) && (curoff != -1 || __errno_r(ptr) == 0))
 		fp->_offset = curoff;
-	      ptr->_errno = tmp_errno;
+	      __errno_r(ptr) = tmp_errno;
 	      if (HASUB (fp))
 		FREEUB (ptr, fp);
 	    }
diff --git a/newlib/libc/stdio/fileno.c b/newlib/libc/stdio/fileno.c
index 10fc6e283..bdb3a050c 100644
--- a/newlib/libc/stdio/fileno.c
+++ b/newlib/libc/stdio/fileno.c
@@ -73,7 +73,7 @@ fileno (FILE * f)
   else
     {
       result = -1;
-      _REENT->_errno = EBADF;
+      __errno_r(_REENT) = EBADF;
     }
   _newlib_flockfile_end (f);
   return result;
diff --git a/newlib/libc/stdio/fileno_u.c b/newlib/libc/stdio/fileno_u.c
index 830a07d2b..d3837ddef 100644
--- a/newlib/libc/stdio/fileno_u.c
+++ b/newlib/libc/stdio/fileno_u.c
@@ -39,7 +39,7 @@ fileno_unlocked (FILE * f)
   else
     {
       result = -1;
-      _REENT->_errno = EBADF;
+      __errno_r(_REENT) = EBADF;
     }
   return result;
 }
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index ee991ed24..7dce3ba89 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -179,7 +179,7 @@ __sfp (struct _reent *d)
 	break;
     }
   _newlib_sfp_lock_exit ();
-  d->_errno = ENOMEM;
+  __errno_r(d) = ENOMEM;
   return NULL;
 
 found:
diff --git a/newlib/libc/stdio/flags.c b/newlib/libc/stdio/flags.c
index 7bbd50181..6c2fcf401 100644
--- a/newlib/libc/stdio/flags.c
+++ b/newlib/libc/stdio/flags.c
@@ -56,7 +56,7 @@ __sflags (struct _reent *ptr,
       o = O_CREAT | O_APPEND;
       break;
     default:			/* illegal mode */
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return (0);
     }
   while (*++mode)
diff --git a/newlib/libc/stdio/fmemopen.c b/newlib/libc/stdio/fmemopen.c
index 0d043520e..a8e441b00 100644
--- a/newlib/libc/stdio/fmemopen.c
+++ b/newlib/libc/stdio/fmemopen.c
@@ -148,7 +148,7 @@ fmemwriter (struct _reent *ptr,
     memcpy (c->buf + c->pos - n, buf, n - adjust);
   else
     {
-      ptr->_errno = ENOSPC;
+      __errno_r(ptr) = ENOSPC;
       return EOF;
     }
   return n;
@@ -175,18 +175,18 @@ fmemseeker (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       offset = -1;
     }
   else if (offset > c->max)
     {
-      ptr->_errno = ENOSPC;
+      __errno_r(ptr) = ENOSPC;
       offset = -1;
     }
 #ifdef __LARGE64_FILES
   else if ((_fpos_t) offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -224,12 +224,12 @@ fmemseeker64 (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       offset = -1;
     }
   else if (offset > c->max)
     {
-      ptr->_errno = ENOSPC;
+      __errno_r(ptr) = ENOSPC;
       offset = -1;
     }
   else
@@ -277,7 +277,7 @@ _fmemopen_r (struct _reent *ptr,
     return NULL;
   if (!size || !(buf || flags & __SRW))
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/fopencookie.c b/newlib/libc/stdio/fopencookie.c
index 0861528e0..576430b7f 100644
--- a/newlib/libc/stdio/fopencookie.c
+++ b/newlib/libc/stdio/fopencookie.c
@@ -107,7 +107,7 @@ fcreader (struct _reent *ptr,
   fccookie *c = (fccookie *) cookie;
   errno = 0;
   if ((result = c->readfn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return result;
 }
 
@@ -129,7 +129,7 @@ fcwriter (struct _reent *ptr,
     }
   errno = 0;
   if ((result = c->writefn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return result;
 }
 
@@ -148,11 +148,11 @@ fcseeker (struct _reent *ptr,
 
   errno = 0;
   if (c->seekfn (c->cookie, &offset, whence) < 0 && errno)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
 #ifdef __LARGE64_FILES
   else if ((_fpos_t)offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -170,7 +170,7 @@ fcseeker64 (struct _reent *ptr,
   fccookie *c = (fccookie *) cookie;
   errno = 0;
   if (c->seekfn (c->cookie, &offset, whence) < 0 && errno)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return (_fpos64_t) offset;
 }
 #endif /* __LARGE64_FILES */
@@ -185,7 +185,7 @@ fccloser (struct _reent *ptr,
     {
       errno = 0;
       if ((result = c->closefn (c->cookie)) < 0 && errno)
-	ptr->_errno = errno;
+	__errno_r(ptr) = errno;
     }
   _free_r (ptr, c);
   return result;
@@ -207,7 +207,7 @@ _fopencookie_r (struct _reent *ptr,
   if (((flags & (__SRD | __SRW)) && !functions.read)
       || ((flags & (__SWR | __SRW)) && !functions.write))
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/fpurge.c b/newlib/libc/stdio/fpurge.c
index 2e4f61ec9..f94f76770 100644
--- a/newlib/libc/stdio/fpurge.c
+++ b/newlib/libc/stdio/fpurge.c
@@ -72,7 +72,7 @@ _fpurge_r (struct _reent *ptr,
   t = fp->_flags;
   if (!t)
     {
-      ptr->_errno = EBADF;
+      __errno_r(ptr) = EBADF;
       _newlib_flockfile_exit (fp);
       return EOF;
     }
diff --git a/newlib/libc/stdio/freopen.c b/newlib/libc/stdio/freopen.c
index 29ba5b7ff..bc4996343 100644
--- a/newlib/libc/stdio/freopen.c
+++ b/newlib/libc/stdio/freopen.c
@@ -137,7 +137,7 @@ _freopen_r (struct _reent *ptr,
   if (file != NULL)
     {
       f = _open_r (ptr, (char *) file, oflags, 0666);
-      e = ptr->_errno;
+      e = __errno_r(ptr);
     }
   else
     {
@@ -205,7 +205,7 @@ _freopen_r (struct _reent *ptr,
     {				/* did not get it after all */
       __sfp_lock_acquire ();
       fp->_flags = 0;		/* set it free */
-      ptr->_errno = e;		/* restore in case _close clobbered */
+      __errno_r(ptr) = e;	/* restore in case _close clobbered */
       if (!(oflags2 & __SNLK))
 	_funlockfile (fp);
 #ifndef __SINGLE_THREAD__
diff --git a/newlib/libc/stdio/fseeko.c b/newlib/libc/stdio/fseeko.c
index 13df28bfc..e26fccb89 100644
--- a/newlib/libc/stdio/fseeko.c
+++ b/newlib/libc/stdio/fseeko.c
@@ -128,7 +128,7 @@ _fseeko_r (struct _reent *ptr,
 
   if ((seekfn = fp->_seek) == NULL)
     {
-      ptr->_errno = ESPIPE;	/* ??? */
+      __errno_r(ptr) = ESPIPE;	/* ??? */
       _newlib_flockfile_exit (fp);
       return EOF;
     }
@@ -178,7 +178,7 @@ _fseeko_r (struct _reent *ptr,
       break;
 
     default:
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       _newlib_flockfile_exit (fp);
       return (EOF);
     }
diff --git a/newlib/libc/stdio/ftell.c b/newlib/libc/stdio/ftell.c
index c540c430f..e4a38674d 100644
--- a/newlib/libc/stdio/ftell.c
+++ b/newlib/libc/stdio/ftell.c
@@ -92,7 +92,7 @@ _ftell_r (struct _reent *ptr,
   if ((long)pos != pos)
     {
       pos = -1;
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
     }
   return (long)pos;
 }
diff --git a/newlib/libc/stdio/ftello.c b/newlib/libc/stdio/ftello.c
index 3df200cea..ee5ea19a3 100644
--- a/newlib/libc/stdio/ftello.c
+++ b/newlib/libc/stdio/ftello.c
@@ -96,7 +96,7 @@ _ftello_r (struct _reent * ptr,
 
   if (fp->_seek == NULL)
     {
-      ptr->_errno = ESPIPE;
+      __errno_r(ptr) = ESPIPE;
       _newlib_flockfile_exit (fp);
       return (_off_t) -1;
     }
diff --git a/newlib/libc/stdio/funopen.c b/newlib/libc/stdio/funopen.c
index a1c0d767d..c9084ca1d 100644
--- a/newlib/libc/stdio/funopen.c
+++ b/newlib/libc/stdio/funopen.c
@@ -113,7 +113,7 @@ funreader (struct _reent *ptr,
   funcookie *c = (funcookie *) cookie;
   errno = 0;
   if ((result = c->readfn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return result;
 }
 
@@ -127,7 +127,7 @@ funwriter (struct _reent *ptr,
   funcookie *c = (funcookie *) cookie;
   errno = 0;
   if ((result = c->writefn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return result;
 }
 
@@ -142,15 +142,15 @@ funseeker (struct _reent *ptr,
   fpos_t result;
   errno = 0;
   if ((result = c->seekfn (c->cookie, (fpos_t) off, whence)) < 0 && errno)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
 #else /* __LARGE64_FILES */
   _fpos64_t result;
   errno = 0;
   if ((result = c->seekfn (c->cookie, (_fpos64_t) off, whence)) < 0 && errno)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   else if ((_fpos_t)result != result)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       result = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -168,7 +168,7 @@ funseeker64 (struct _reent *ptr,
   funcookie *c = (funcookie *) cookie;
   errno = 0;
   if ((result = c->seekfn (c->cookie, off, whence)) < 0 && errno)
-    ptr->_errno = errno;
+    __errno_r(ptr) = errno;
   return result;
 }
 #endif /* __LARGE64_FILES */
@@ -183,7 +183,7 @@ funcloser (struct _reent *ptr,
     {
       errno = 0;
       if ((result = c->closefn (c->cookie)) < 0 && errno)
-	ptr->_errno = errno;
+	__errno_r(ptr) = errno;
     }
   _free_r (ptr, c);
   return result;
@@ -202,7 +202,7 @@ _funopen_r (struct _reent *ptr,
 
   if (!readfn && !writefn)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/fvwrite.c b/newlib/libc/stdio/fvwrite.c
index 587ffacd7..90fa491b7 100644
--- a/newlib/libc/stdio/fvwrite.c
+++ b/newlib/libc/stdio/fvwrite.c
@@ -145,7 +145,7 @@ __sfvwrite_r (struct _reent *ptr,
 		      str = (unsigned char *)_malloc_r (ptr, newsize);
 		      if (!str)
 			{
-			  ptr->_errno = ENOMEM;
+			  __errno_r(ptr) = ENOMEM;
 			  goto err;
 			}
 		      memcpy (str, fp->_bf._base, curpos);
@@ -162,7 +162,7 @@ __sfvwrite_r (struct _reent *ptr,
 			  _free_r (ptr, fp->_bf._base);
 			  fp->_flags &=  ~__SMBF;
 			  /* Ensure correct errno, even if free changed it.  */
-			  ptr->_errno = ENOMEM;
+			  __errno_r(ptr) = ENOMEM;
 			  goto err;
 			}
 		    }
diff --git a/newlib/libc/stdio/mktemp.c b/newlib/libc/stdio/mktemp.c
index 3514818a6..da30f0be7 100644
--- a/newlib/libc/stdio/mktemp.c
+++ b/newlib/libc/stdio/mktemp.c
@@ -159,7 +159,7 @@ _gettemp (struct _reent *ptr,
     continue;
   if (trv - path < suffixlen)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return 0;
     }
   trv -= suffixlen;
@@ -171,7 +171,7 @@ _gettemp (struct _reent *ptr,
     }
   if (end - trv < 6)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return 0;
     }
 
@@ -195,7 +195,7 @@ _gettemp (struct _reent *ptr,
 	    return (0);
 	  if (!(sbuf.st_mode & S_IFDIR))
 	    {
-	      ptr->_errno = ENOTDIR;
+	      __errno_r(ptr) = ENOTDIR;
 	      return (0);
 	    }
 	  *trv = '/';
@@ -211,10 +211,10 @@ _gettemp (struct _reent *ptr,
 #ifdef HAVE_MKDIR
 	  if (_mkdir_r (ptr, path, 0700) == 0)
 	    return 1;
-	  if (ptr->_errno != EEXIST)
+	  if (__errno_r(ptr) != EEXIST)
 	    return 0;
 #else /* !HAVE_MKDIR */
-	  ptr->_errno = ENOSYS;
+	  __errno_r(ptr) = ENOSYS;
 	  return 0;
 #endif /* !HAVE_MKDIR */
 	}
@@ -225,7 +225,7 @@ _gettemp (struct _reent *ptr,
 	  if ((*doopen = _open_r (ptr, path, O_CREAT | O_EXCL | O_RDWR | flags,
 				  0600)) >= 0)
 	    return 1;
-	  if (ptr->_errno != EEXIST)
+	  if (__errno_r(ptr) != EEXIST)
 	    return 0;
 	}
 #ifdef __USE_INTERNAL_STAT64
@@ -233,7 +233,7 @@ _gettemp (struct _reent *ptr,
 #else
       else if (_stat_r (ptr, path, &sbuf))
 #endif
-	return (ptr->_errno == ENOENT ? 1 : 0);
+	return (__errno_r(ptr) == ENOENT ? 1 : 0);
 
       /* tricky little algorithm for backward compatibility */
       for (trv = start;;)
diff --git a/newlib/libc/stdio/nano-vfprintf.c b/newlib/libc/stdio/nano-vfprintf.c
index 838804eb9..60beda610 100644
--- a/newlib/libc/stdio/nano-vfprintf.c
+++ b/newlib/libc/stdio/nano-vfprintf.c
@@ -201,7 +201,7 @@ __ssputs_r (struct _reent *ptr,
 	  str = (unsigned char *)_malloc_r (ptr, newsize);
 	  if (!str)
 	    {
-	      ptr->_errno = ENOMEM;
+	      __errno_r(ptr) = ENOMEM;
 	      goto err;
 	    }
 	  memcpy (str, fp->_bf._base, curpos);
@@ -215,7 +215,7 @@ __ssputs_r (struct _reent *ptr,
 	      /* Free unneeded buffer.  */
 	      _free_r (ptr, fp->_bf._base);
 	      /* Ensure correct errno, even if free changed it.  */
-	      ptr->_errno = ENOMEM;
+	      __errno_r(ptr) = ENOMEM;
 	      goto err;
 	    }
 	}
@@ -291,7 +291,7 @@ __ssprint_r (struct _reent *ptr,
 	      str = (unsigned char *)_malloc_r (ptr, newsize);
 	      if (!str)
 		{
-		  ptr->_errno = ENOMEM;
+		  __errno_r(ptr) = ENOMEM;
 		  goto err;
 		}
 	      memcpy (str, fp->_bf._base, curpos);
@@ -306,7 +306,7 @@ __ssprint_r (struct _reent *ptr,
 		  /* Free unneeded buffer.  */
 		  _free_r (ptr, fp->_bf._base);
 		  /* Ensure correct errno, even if free changed it.  */
-		  ptr->_errno = ENOMEM;
+		  __errno_r(ptr) = ENOMEM;
 		  goto err;
 		}
 	    }
@@ -501,7 +501,7 @@ _VFPRINTF_R (struct _reent *data,
       fp->_bf._base = fp->_p = _malloc_r (data, 64);
       if (!fp->_p)
 	{
-	  data->_errno = ENOMEM;
+	  __errno_r(data) = ENOMEM;
 	  return EOF;
 	}
       fp->_bf._size = 64;
diff --git a/newlib/libc/stdio/open_memstream.c b/newlib/libc/stdio/open_memstream.c
index ed71b1cdc..a4dac6713 100644
--- a/newlib/libc/stdio/open_memstream.c
+++ b/newlib/libc/stdio/open_memstream.c
@@ -105,7 +105,7 @@ memwriter (struct _reent *ptr,
      big that user cannot do ftello.  */
   if (sizeof (OFF_T) == sizeof (size_t) && (ssize_t) (c->pos + n) < 0)
     {
-      ptr->_errno = EFBIG;
+      __errno_r(ptr) = EFBIG;
       return EOF;
     }
   /* Grow the buffer, if necessary.  Choose a geometric growth factor
@@ -160,18 +160,18 @@ memseeker (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       offset = -1;
     }
   else if ((size_t) offset != offset)
     {
-      ptr->_errno = ENOSPC;
+      __errno_r(ptr) = ENOSPC;
       offset = -1;
     }
 #ifdef __LARGE64_FILES
   else if ((_fpos_t) offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -227,12 +227,12 @@ memseeker64 (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       offset = -1;
     }
   else if ((size_t) offset != offset)
     {
-      ptr->_errno = ENOSPC;
+      __errno_r(ptr) = ENOSPC;
       offset = -1;
     }
   else
@@ -301,7 +301,7 @@ internal_open_memstream_r (struct _reent *ptr,
 
   if (!buf || !size)
     {
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/perror.c b/newlib/libc/stdio/perror.c
index eda7e993b..fbbb27de4 100644
--- a/newlib/libc/stdio/perror.c
+++ b/newlib/libc/stdio/perror.c
@@ -90,7 +90,7 @@ _perror_r (struct _reent *ptr,
       WRITE_STR (": ");
     }
 
-  if ((error = _strerror_r (ptr, ptr->_errno, 1, &dummy)) != NULL)
+  if ((error = _strerror_r (ptr, __errno_r(ptr), 1, &dummy)) != NULL)
     WRITE_STR (error);
 
 #ifdef __SCLE
diff --git a/newlib/libc/stdio/refill.c b/newlib/libc/stdio/refill.c
index 91e79e832..7abbbbf23 100644
--- a/newlib/libc/stdio/refill.c
+++ b/newlib/libc/stdio/refill.c
@@ -56,7 +56,7 @@ __srefill_r (struct _reent * ptr,
     {
       if ((fp->_flags & __SRW) == 0)
 	{
-	  ptr->_errno = EBADF;
+	  __errno_r(ptr) = EBADF;
 	  fp->_flags |= __SERR;
 	  return EOF;
 	}
diff --git a/newlib/libc/stdio/sniprintf.c b/newlib/libc/stdio/sniprintf.c
index 375a3971f..c25d189fd 100644
--- a/newlib/libc/stdio/sniprintf.c
+++ b/newlib/libc/stdio/sniprintf.c
@@ -38,7 +38,7 @@ _sniprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -49,7 +49,7 @@ _sniprintf_r (struct _reent *ptr,
   ret = _svfiprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    __errno_r(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
@@ -69,7 +69,7 @@ sniprintf (char *str,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -80,7 +80,7 @@ sniprintf (char *str,
   ret = _svfiprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    __errno_r(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
diff --git a/newlib/libc/stdio/snprintf.c b/newlib/libc/stdio/snprintf.c
index 50d7ddd11..f32806069 100644
--- a/newlib/libc/stdio/snprintf.c
+++ b/newlib/libc/stdio/snprintf.c
@@ -37,7 +37,7 @@ _snprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -48,7 +48,7 @@ _snprintf_r (struct _reent *ptr,
   ret = _svfprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    __errno_r(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
@@ -74,7 +74,7 @@ snprintf (char *__restrict str,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -85,7 +85,7 @@ snprintf (char *__restrict str,
   ret = _svfprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    __errno_r(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
diff --git a/newlib/libc/stdio/swprintf.c b/newlib/libc/stdio/swprintf.c
index ec7e089ca..c831cfbe1 100644
--- a/newlib/libc/stdio/swprintf.c
+++ b/newlib/libc/stdio/swprintf.c
@@ -564,7 +564,7 @@ _swprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX / sizeof (wchar_t))
     {
-      ptr->_errno = EOVERFLOW;	/* POSIX extension */
+      __errno_r(ptr) = EOVERFLOW;	/* POSIX extension */
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -584,7 +584,7 @@ _swprintf_r (struct _reent *ptr,
     /* _svfwprintf_r() returns how many wide characters it would have printed
      * if there were enough space.  Return an error if too big to fit in str,
      * unlike snprintf, which returns the size needed.  */
-    ptr->_errno = EOVERFLOW;	/* POSIX extension */
+    __errno_r(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return (ret);
@@ -604,7 +604,7 @@ swprintf (wchar_t *__restrict str,
 
   if (size > INT_MAX / sizeof (wchar_t))
     {
-      ptr->_errno = EOVERFLOW;	/* POSIX extension */
+      __errno_r(ptr) = EOVERFLOW;	/* POSIX extension */
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -624,7 +624,7 @@ swprintf (wchar_t *__restrict str,
     /* _svfwprintf_r() returns how many wide characters it would have printed
      * if there were enough space.  Return an error if too big to fit in str,
      * unlike snprintf, which returns the size needed.  */
-    ptr->_errno = EOVERFLOW;	/* POSIX extension */
+    __errno_r(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return (ret);
diff --git a/newlib/libc/stdio/tmpfile.c b/newlib/libc/stdio/tmpfile.c
index f209a3edf..79fe69f6d 100644
--- a/newlib/libc/stdio/tmpfile.c
+++ b/newlib/libc/stdio/tmpfile.c
@@ -65,15 +65,15 @@ _tmpfile_r (struct _reent *ptr)
       fd = _open_r (ptr, f, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
 		    S_IRUSR | S_IWUSR);
     }
-  while (fd < 0 && ptr->_errno == EEXIST);
+  while (fd < 0 && __errno_r(ptr) == EEXIST);
   if (fd < 0)
     return NULL;
   fp = _fdopen_r (ptr, fd, "wb+");
-  e = ptr->_errno;
+  e = __errno_r(ptr);
   if (!fp)
     _close_r (ptr, fd);
   (void) _remove_r (ptr, f);
-  ptr->_errno = e;
+  __errno_r(ptr) = e;
   return fp;
 }
 
diff --git a/newlib/libc/stdio/tmpnam.c b/newlib/libc/stdio/tmpnam.c
index 3bd5b6a58..607e784b8 100644
--- a/newlib/libc/stdio/tmpnam.c
+++ b/newlib/libc/stdio/tmpnam.c
@@ -104,7 +104,7 @@ worker (struct _reent *ptr,
       t = _open_r (ptr, result, O_RDONLY, 0);
       if (t == -1)
 	{
-	  if (ptr->_errno == ENOSYS)
+	  if (__errno_r(ptr) == ENOSYS)
 	    {
 	      result[0] = '\0';
 	      return 0;
diff --git a/newlib/libc/stdio/vasniprintf.c b/newlib/libc/stdio/vasniprintf.c
index edfbd2242..69ce67239 100644
--- a/newlib/libc/stdio/vasniprintf.c
+++ b/newlib/libc/stdio/vasniprintf.c
@@ -42,7 +42,7 @@ _vasniprintf_r (struct _reent *ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/vasnprintf.c b/newlib/libc/stdio/vasnprintf.c
index 1fbf5a171..f21ab6c2a 100644
--- a/newlib/libc/stdio/vasnprintf.c
+++ b/newlib/libc/stdio/vasnprintf.c
@@ -42,7 +42,7 @@ _vasnprintf_r (struct _reent *ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/vfprintf.c b/newlib/libc/stdio/vfprintf.c
index 32ebb1468..113c0fd11 100644
--- a/newlib/libc/stdio/vfprintf.c
+++ b/newlib/libc/stdio/vfprintf.c
@@ -222,7 +222,7 @@ __ssputs_r (struct _reent *ptr,
 			str = (unsigned char *)_malloc_r (ptr, newsize);
 			if (!str)
 			{
-				ptr->_errno = ENOMEM;
+				__errno_r(ptr) = ENOMEM;
 				goto err;
 			}
 			memcpy (str, fp->_bf._base, curpos);
@@ -237,7 +237,7 @@ __ssputs_r (struct _reent *ptr,
 				_free_r (ptr, fp->_bf._base);
 				/* Ensure correct errno, even if free
 				 * changed it.  */
-				ptr->_errno = ENOMEM;
+				__errno_r(ptr) = ENOMEM;
 				goto err;
 			}
 		}
@@ -306,7 +306,7 @@ __ssprint_r (struct _reent *ptr,
 				str = (unsigned char *)_malloc_r (ptr, newsize);
 				if (!str)
 				{
-					ptr->_errno = ENOMEM;
+					__errno_r(ptr) = ENOMEM;
 					goto err;
 				}
 				memcpy (str, fp->_bf._base, curpos);
@@ -321,7 +321,7 @@ __ssprint_r (struct _reent *ptr,
 					_free_r (ptr, fp->_bf._base);
 					/* Ensure correct errno, even if free
 					 * changed it.  */
-					ptr->_errno = ENOMEM;
+					__errno_r(ptr) = ENOMEM;
 					goto err;
 				}
 			}
@@ -868,7 +868,7 @@ _VFPRINTF_R (struct _reent *data,
 		fp->_bf._base = fp->_p = _malloc_r (data, 64);
 		if (!fp->_p)
 		{
-			data->_errno = ENOMEM;
+			__errno_r(data) = ENOMEM;
 			return EOF;
 		}
 		fp->_bf._size = 64;
@@ -1374,7 +1374,7 @@ reswitch:	switch (ch) {
 		case 'm':  /* extension */
 			{
 				int dummy;
-				cp = _strerror_r (data, data->_errno, 1, &dummy);
+				cp = _strerror_r (data, __errno_r(data), 1, &dummy);
 			}
 			flags &= ~LONGINT;
 			goto string;
diff --git a/newlib/libc/stdio/vfscanf.c b/newlib/libc/stdio/vfscanf.c
index 037692531..a171b2188 100644
--- a/newlib/libc/stdio/vfscanf.c
+++ b/newlib/libc/stdio/vfscanf.c
@@ -774,7 +774,7 @@ __SVFSCANF_R (struct _reent *rptr,
 	      width = 0;
 	      goto again;
 	    }
-	  rptr->_errno = EINVAL;
+	  __errno_r(rptr) = EINVAL;
 	  goto input_failure;
 #endif /* !_NO_POS_ARGS */
 
diff --git a/newlib/libc/stdio/vfwprintf.c b/newlib/libc/stdio/vfwprintf.c
index 7384b37d3..7d65da626 100644
--- a/newlib/libc/stdio/vfwprintf.c
+++ b/newlib/libc/stdio/vfwprintf.c
@@ -611,7 +611,7 @@ _VFWPRINTF_R (struct _reent *data,
 		fp->_bf._base = fp->_p = _malloc_r (data, 64);
 		if (!fp->_p)
 		{
-			data->_errno = ENOMEM;
+			__errno_r(data) = ENOMEM;
 			return EOF;
 		}
 		fp->_bf._size = 64;
@@ -1119,7 +1119,7 @@ reswitch:	switch (ch) {
 		case L'm':  /* GNU extension */
 			{
 				int dummy;
-				cp = (wchar_t *) _strerror_r (data, data->_errno, 1, &dummy);
+				cp = (wchar_t *) _strerror_r (data, __errno_r(data), 1, &dummy);
 			}
 			flags &= ~LONGINT;
 			goto string;
diff --git a/newlib/libc/stdio/vfwscanf.c b/newlib/libc/stdio/vfwscanf.c
index e9e00dfec..78d19b2f2 100644
--- a/newlib/libc/stdio/vfwscanf.c
+++ b/newlib/libc/stdio/vfwscanf.c
@@ -682,7 +682,7 @@ __SVFWSCANF_R (struct _reent *rptr,
 	      width = 0;
 	      goto again;
 	    }
-	  rptr->_errno = EINVAL;
+	  __errno_r(rptr) = EINVAL;
 	  goto input_failure;
 #endif /* !_NO_POS_ARGS */
 
diff --git a/newlib/libc/stdio/vsniprintf.c b/newlib/libc/stdio/vsniprintf.c
index a8b272e2f..a12576539 100644
--- a/newlib/libc/stdio/vsniprintf.c
+++ b/newlib/libc/stdio/vsniprintf.c
@@ -54,7 +54,7 @@ _vsniprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -63,7 +63,7 @@ _vsniprintf_r (struct _reent *ptr,
   f._file = -1;  /* No file. */
   ret = _svfiprintf_r (ptr, &f, fmt, ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    __errno_r(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return ret;
diff --git a/newlib/libc/stdio/vsnprintf.c b/newlib/libc/stdio/vsnprintf.c
index 5c617a8a3..d1a28ad81 100644
--- a/newlib/libc/stdio/vsnprintf.c
+++ b/newlib/libc/stdio/vsnprintf.c
@@ -60,7 +60,7 @@ _vsnprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      __errno_r(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -69,7 +69,7 @@ _vsnprintf_r (struct _reent *ptr,
   f._file = -1;  /* No file. */
   ret = _svfprintf_r (ptr, &f, fmt, ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    __errno_r(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return ret;
diff --git a/newlib/libc/stdio/vswprintf.c b/newlib/libc/stdio/vswprintf.c
index 89795ed8d..914b365a2 100644
--- a/newlib/libc/stdio/vswprintf.c
+++ b/newlib/libc/stdio/vswprintf.c
@@ -42,7 +42,7 @@ _vswprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX / sizeof (wchar_t))
     {
-      ptr->_errno = EOVERFLOW;	/* POSIX extension */
+      __errno_r(ptr) = EOVERFLOW;	/* POSIX extension */
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -60,7 +60,7 @@ _vswprintf_r (struct _reent *ptr,
     /* _svfwprintf_r() returns how many wide characters it would have printed
      * if there were enough space.  Return an error if too big to fit in str,
      * unlike snprintf, which returns the size needed.  */
-    ptr->_errno = EOVERFLOW;	/* POSIX extension */
+    __errno_r(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return ret;
diff --git a/newlib/libc/stdio/wsetup.c b/newlib/libc/stdio/wsetup.c
index 6a820f1c0..6e6519767 100644
--- a/newlib/libc/stdio/wsetup.c
+++ b/newlib/libc/stdio/wsetup.c
@@ -45,7 +45,7 @@ __swsetup_r (struct _reent *ptr,
     {
       if ((fp->_flags & __SRW) == 0)
         {
-	  ptr->_errno = EBADF;
+	  __errno_r(ptr) = EBADF;
 	  fp->_flags |= __SERR;
 	  return EOF;
         }
diff --git a/newlib/libc/stdio64/fdopen64.c b/newlib/libc/stdio64/fdopen64.c
index 9d9645b36..3774c913f 100644
--- a/newlib/libc/stdio64/fdopen64.c
+++ b/newlib/libc/stdio64/fdopen64.c
@@ -55,7 +55,7 @@ _fdopen64_r (struct _reent *ptr,
   fdmode = fdflags & O_ACCMODE;
   if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
     {
-      ptr->_errno = EBADF;
+      __errno_r(ptr) = EBADF;
       return 0;
     }
 #endif
diff --git a/newlib/libc/stdio64/freopen64.c b/newlib/libc/stdio64/freopen64.c
index e6ba64f7d..5b4899f67 100644
--- a/newlib/libc/stdio64/freopen64.c
+++ b/newlib/libc/stdio64/freopen64.c
@@ -138,7 +138,7 @@ _freopen64_r (struct _reent *ptr,
   if (file != NULL)
     {
       f = _open64_r (ptr, (char *) file, oflags, 0666);
-      e = ptr->_errno;
+      e = __errno_r(ptr);
     }
   else
     {
@@ -206,7 +206,7 @@ _freopen64_r (struct _reent *ptr,
     {				/* did not get it after all */
       __sfp_lock_acquire ();
       fp->_flags = 0;		/* set it free */
-      ptr->_errno = e;		/* restore in case _close clobbered */
+      __errno_r(ptr) = e;		/* restore in case _close clobbered */
       if (!(oflags2 & __SNLK))
 	_funlockfile (fp);
 #ifndef __SINGLE_THREAD__
diff --git a/newlib/libc/stdio64/fseeko64.c b/newlib/libc/stdio64/fseeko64.c
index 3087bef9e..0510e3582 100644
--- a/newlib/libc/stdio64/fseeko64.c
+++ b/newlib/libc/stdio64/fseeko64.c
@@ -102,7 +102,7 @@ _fseeko64_r (struct _reent *ptr,
     {
       if ((_off_t) offset != offset)
 	{
-	  ptr->_errno = EOVERFLOW;
+	  __errno_r(ptr) = EOVERFLOW;
 	  return EOF;
 	}
       return (_off64_t) _fseeko_r (ptr, fp, offset, whence);
@@ -129,7 +129,7 @@ _fseeko64_r (struct _reent *ptr,
 
   if ((seekfn = fp->_seek64) == NULL)
     {
-      ptr->_errno = ESPIPE;	/* ??? */
+      __errno_r(ptr) = ESPIPE;	/* ??? */
       _newlib_flockfile_exit(fp);
       return EOF;
     }
@@ -179,7 +179,7 @@ _fseeko64_r (struct _reent *ptr,
       break;
 
     default:
-      ptr->_errno = EINVAL;
+      __errno_r(ptr) = EINVAL;
       _newlib_flockfile_exit(fp);
       return (EOF);
     }
diff --git a/newlib/libc/stdio64/ftello64.c b/newlib/libc/stdio64/ftello64.c
index 6fb76c3ef..97c7e96b8 100644
--- a/newlib/libc/stdio64/ftello64.c
+++ b/newlib/libc/stdio64/ftello64.c
@@ -93,7 +93,7 @@ _ftello64_r (struct _reent *ptr,
 
   if (fp->_seek64 == NULL)
     {
-      ptr->_errno = ESPIPE;
+      __errno_r(ptr) = ESPIPE;
       _newlib_flockfile_exit(fp);
       return (_off64_t) -1;
     }
diff --git a/newlib/libc/stdio64/tmpfile64.c b/newlib/libc/stdio64/tmpfile64.c
index 18a38d65c..d8fe55605 100644
--- a/newlib/libc/stdio64/tmpfile64.c
+++ b/newlib/libc/stdio64/tmpfile64.c
@@ -68,15 +68,15 @@ _tmpfile64_r (struct _reent *ptr)
       fd = _open64_r (ptr, f, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
 		      S_IRUSR | S_IWUSR);
   }
-  while (fd < 0 && ptr->_errno == EEXIST);
+  while (fd < 0 && __errno_r(ptr) == EEXIST);
   if (fd < 0)
     return NULL;
   fp = _fdopen64_r (ptr, fd, "wb+");
-  e = ptr->_errno;
+  e = __errno_r(ptr);
   if (!fp)
     _close_r (ptr, fd);
   (void) _remove_r (ptr, f);
-  ptr->_errno = e;
+  __errno_r(ptr) = e;
   return fp;
 }
 
diff --git a/newlib/libc/stdlib/__adjust.c b/newlib/libc/stdlib/__adjust.c
index 4c478f30b..9b38e8d3e 100644
--- a/newlib/libc/stdlib/__adjust.c
+++ b/newlib/libc/stdlib/__adjust.c
@@ -21,12 +21,12 @@ __adjust (struct _reent *ptr,
 
   if (dexp > MAXE)
     {
-      ptr->_errno = ERANGE;
+      __errno_r(ptr) = ERANGE;
       return (sign) ? -HUGE_VAL : HUGE_VAL;
     }
   else if (dexp < MINE)
     {
-      ptr->_errno = ERANGE;
+      __errno_r(ptr) = ERANGE;
       return 0.0;
     }
 
diff --git a/newlib/libc/stdlib/mbrtowc.c b/newlib/libc/stdlib/mbrtowc.c
index 65284a0eb..b43d8ea6a 100644
--- a/newlib/libc/stdlib/mbrtowc.c
+++ b/newlib/libc/stdlib/mbrtowc.c
@@ -32,7 +32,7 @@ _mbrtowc_r (struct _reent *ptr,
   if (retval == -1)
     {
       ps->__count = 0;
-      ptr->_errno = EILSEQ;
+      __errno_r(ptr) = EILSEQ;
       return (size_t)(-1);
     }
   else
@@ -68,7 +68,7 @@ mbrtowc (wchar_t *__restrict pwc,
   if (retval == -1)
     {
       ps->__count = 0;
-      reent->_errno = EILSEQ;
+      __errno_r(reent) = EILSEQ;
       return (size_t)(-1);
     }
   else
diff --git a/newlib/libc/stdlib/mbsnrtowcs.c b/newlib/libc/stdlib/mbsnrtowcs.c
index d3ce25084..e4221d75e 100644
--- a/newlib/libc/stdlib/mbsnrtowcs.c
+++ b/newlib/libc/stdlib/mbsnrtowcs.c
@@ -126,7 +126,7 @@ _mbsnrtowcs_r (struct _reent *r,
       else
 	{
 	  ps->__count = 0;
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return (size_t)-1;
 	}
     }
diff --git a/newlib/libc/stdlib/mbtowc_r.c b/newlib/libc/stdlib/mbtowc_r.c
index 920a7ea3c..eb50fa6da 100644
--- a/newlib/libc/stdlib/mbtowc_r.c
+++ b/newlib/libc/stdlib/mbtowc_r.c
@@ -39,7 +39,7 @@ __ascii_mbtowc (struct _reent *r,
 #ifdef __CYGWIN__
   if ((wchar_t)*t >= 0x80)
     {
-      r->_errno = EILSEQ;
+      __errno_r(r) = EILSEQ;
       return -1;
     }
 #endif
@@ -117,7 +117,7 @@ ___iso_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
 	  *pwc = __iso_8859_conv[iso_idx][*t - 0xa0];
 	  if (*pwc == 0) /* Invalid character */
 	    {
-	      r->_errno = EILSEQ;
+	      __errno_r(r) = EILSEQ;
 	      return -1;
 	    }
 	  return 1;
@@ -290,7 +290,7 @@ ___cp_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
 	  *pwc = __cp_conv[cp_idx][*t - 0x80];
 	  if (*pwc == 0) /* Invalid character */
 	    {
-	      r->_errno = EILSEQ;
+	      __errno_r(r) = EILSEQ;
 	      return -1;
 	    }
 	  return 1;
@@ -578,13 +578,13 @@ __utf8_mbtowc (struct _reent *r,
       ch = t[i++];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
       if (state->__value.__wchb[0] < 0xc2)
 	{
 	  /* overlong UTF-8 sequence */
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
       state->__count = 0;
@@ -607,12 +607,12 @@ __utf8_mbtowc (struct _reent *r,
       if (state->__value.__wchb[0] == 0xe0 && ch < 0xa0)
 	{
 	  /* overlong UTF-8 sequence */
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
       state->__value.__wchb[1] = ch;
@@ -625,7 +625,7 @@ __utf8_mbtowc (struct _reent *r,
       ch = t[i++];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
       state->__count = 0;
@@ -651,12 +651,12 @@ __utf8_mbtowc (struct _reent *r,
 	  || (state->__value.__wchb[0] == 0xf4 && ch >= 0x90))
 	{
 	  /* overlong UTF-8 sequence or result is > 0x10ffff */
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
       state->__value.__wchb[1] = ch;
@@ -669,7 +669,7 @@ __utf8_mbtowc (struct _reent *r,
       ch = (state->__count == 2) ? t[i++] : state->__value.__wchb[2];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
       state->__value.__wchb[2] = ch;
@@ -702,7 +702,7 @@ __utf8_mbtowc (struct _reent *r,
       ch = t[i++];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
       tmp = (wint_t)((state->__value.__wchb[0] & 0x07) << 18)
@@ -719,7 +719,7 @@ __utf8_mbtowc (struct _reent *r,
       return i;
     }
 
-  r->_errno = EILSEQ;
+  __errno_r(r) = EILSEQ;
   return -1;
 }
 
@@ -769,7 +769,7 @@ __sjis_mbtowc (struct _reent *r,
 	}
       else  
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -836,7 +836,7 @@ __eucjp_mbtowc (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -851,7 +851,7 @@ __eucjp_mbtowc (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -955,7 +955,7 @@ __jis_mbtowc (struct _reent *r,
 	  break;
 	case ERROR:
 	default:
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
 
diff --git a/newlib/libc/stdlib/strtod.c b/newlib/libc/stdlib/strtod.c
index cd0222481..91fc756d1 100644
--- a/newlib/libc/stdlib/strtod.c
+++ b/newlib/libc/stdlib/strtod.c
@@ -595,7 +595,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se,
 			if (e1 > DBL_MAX_10_EXP) {
  ovfl:
 #ifndef NO_ERRNO
-				ptr->_errno = ERANGE;
+				__errno_r(ptr) = ERANGE;
 #endif
 				/* Can't trust HUGE_VAL */
 #ifdef IEEE_Arith
@@ -702,7 +702,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se,
  undfl:
 					dval(rv) = 0.;
 #ifndef NO_ERRNO
-					ptr->_errno = ERANGE;
+					__errno_r(ptr) = ERANGE;
 #endif
 					if (bd0)
 						goto retfree;
@@ -1249,7 +1249,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se,
 #ifndef NO_ERRNO
 		/* try to avoid the bug of testing an 8087 register value */
 		if ((dword0(rv) & Exp_mask) == 0)
-			ptr->_errno = ERANGE;
+			__errno_r(ptr) = ERANGE;
 #endif
 		}
 #endif /* Avoid_Underflow */
@@ -1303,7 +1303,7 @@ strtof_l (const char *__restrict s00, char **__restrict se, locale_t loc)
   float retval = (float) val;
 #ifndef NO_ERRNO
   if (isinf (retval) && !isinf (val))
-    _REENT->_errno = ERANGE;
+    __errno_r(_REENT) = ERANGE;
 #endif
   return retval;
 }
@@ -1340,7 +1340,7 @@ strtof (const char *__restrict s00,
   float retval = (float) val;
 #ifndef NO_ERRNO
   if ((isinf (retval) && !isinf (val)) || (isdenormf(retval) && !isdenorm(val)))
-    _REENT->_errno = ERANGE;
+    __errno_r(_REENT) = ERANGE;
 #endif
   return retval;
 }
diff --git a/newlib/libc/stdlib/strtoimax.c b/newlib/libc/stdlib/strtoimax.c
index c3f27df2e..5aed095af 100644
--- a/newlib/libc/stdlib/strtoimax.c
+++ b/newlib/libc/stdlib/strtoimax.c
@@ -136,10 +136,10 @@ _strtoimax_l(struct _reent *rptr, const char * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? INTMAX_MIN : INTMAX_MAX;
-		rptr->_errno = ERANGE;
+		__errno_r(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		__errno_r(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/strtol.c b/newlib/libc/stdlib/strtol.c
index 6383c27e8..8af07c4da 100644
--- a/newlib/libc/stdlib/strtol.c
+++ b/newlib/libc/stdlib/strtol.c
@@ -204,7 +204,7 @@ _strtol_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_MIN : LONG_MAX;
-		rptr->_errno = ERANGE;
+		__errno_r(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoll.c b/newlib/libc/stdlib/strtoll.c
index 9aa2c747b..5d87e6593 100644
--- a/newlib/libc/stdlib/strtoll.c
+++ b/newlib/libc/stdlib/strtoll.c
@@ -201,7 +201,7 @@ _strtoll_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_LONG_MIN : LONG_LONG_MAX;
-		rptr->_errno = ERANGE;
+		__errno_r(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoul.c b/newlib/libc/stdlib/strtoul.c
index 4191e43ec..accd2852d 100644
--- a/newlib/libc/stdlib/strtoul.c
+++ b/newlib/libc/stdlib/strtoul.c
@@ -178,7 +178,7 @@ _strtoul_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = ULONG_MAX;
-		rptr->_errno = ERANGE;
+		__errno_r(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoull.c b/newlib/libc/stdlib/strtoull.c
index 10018ca0d..2c8000a65 100644
--- a/newlib/libc/stdlib/strtoull.c
+++ b/newlib/libc/stdlib/strtoull.c
@@ -176,7 +176,7 @@ _strtoull_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = ULONG_LONG_MAX;
-		rptr->_errno = ERANGE;
+		__errno_r(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoumax.c b/newlib/libc/stdlib/strtoumax.c
index cf1a427fe..57629eb03 100644
--- a/newlib/libc/stdlib/strtoumax.c
+++ b/newlib/libc/stdlib/strtoumax.c
@@ -115,10 +115,10 @@ _strtoumax_l(struct _reent *rptr, const char * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = UINTMAX_MAX;
-		rptr->_errno = ERANGE;
+		__errno_r(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		__errno_r(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/wcrtomb.c b/newlib/libc/stdlib/wcrtomb.c
index 97436cb74..73a7404be 100644
--- a/newlib/libc/stdlib/wcrtomb.c
+++ b/newlib/libc/stdlib/wcrtomb.c
@@ -31,7 +31,7 @@ _wcrtomb_r (struct _reent *ptr,
   if (retval == -1)
     {
       ps->__count = 0;
-      ptr->_errno = EILSEQ;
+      __errno_r(ptr) = EILSEQ;
       return (size_t)(-1);
     }
   else
@@ -67,7 +67,7 @@ wcrtomb (char *__restrict s,
   if (retval == -1)
     {
       ps->__count = 0;
-      reent->_errno = EILSEQ;
+      __errno_r(reent) = EILSEQ;
       return (size_t)(-1);
     }
   else
diff --git a/newlib/libc/stdlib/wcsnrtombs.c b/newlib/libc/stdlib/wcsnrtombs.c
index 43dd2f3e6..e58a1c3b2 100644
--- a/newlib/libc/stdlib/wcsnrtombs.c
+++ b/newlib/libc/stdlib/wcsnrtombs.c
@@ -104,7 +104,7 @@ _wcsnrtombs_l (struct _reent *r, char *dst, const wchar_t **src, size_t nwc,
       int bytes = loc->wctomb (r, buff, *pwcs, ps);
       if (bytes == -1)
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  ps->__count = 0;
 	  return (size_t)-1;
 	}
diff --git a/newlib/libc/stdlib/wcstod.c b/newlib/libc/stdlib/wcstod.c
index 375ffe288..17c5c4e8a 100644
--- a/newlib/libc/stdlib/wcstod.c
+++ b/newlib/libc/stdlib/wcstod.c
@@ -257,7 +257,7 @@ wcstof_l (const wchar_t *__restrict nptr, wchar_t **__restrict endptr,
   float retval = (float) val;
 #ifndef NO_ERRNO
   if (isinf (retval) && !isinf (val))
-    _REENT->_errno = ERANGE;
+    __errno_r(_REENT) = ERANGE;
 #endif
   return retval;
 }
@@ -272,7 +272,7 @@ wcstof (const wchar_t *__restrict nptr,
   float retval = (float) val;
 #ifndef NO_ERRNO
   if (isinf (retval) && !isinf (val))
-    _REENT->_errno = ERANGE;
+    __errno_r(_REENT) = ERANGE;
 #endif
 
   return retval;
diff --git a/newlib/libc/stdlib/wcstoimax.c b/newlib/libc/stdlib/wcstoimax.c
index 02ab1c1f4..2542048ca 100644
--- a/newlib/libc/stdlib/wcstoimax.c
+++ b/newlib/libc/stdlib/wcstoimax.c
@@ -122,10 +122,10 @@ _wcstoimax_l(struct _reent *rptr, const wchar_t * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? INTMAX_MIN : INTMAX_MAX;
-		rptr->_errno = ERANGE;
+		__errno_r(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		__errno_r(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/wcstol.c b/newlib/libc/stdlib/wcstol.c
index 8b6de3873..cafc609c8 100644
--- a/newlib/libc/stdlib/wcstol.c
+++ b/newlib/libc/stdlib/wcstol.c
@@ -200,7 +200,7 @@ _wcstol_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_MIN : LONG_MAX;
-		rptr->_errno = ERANGE;
+		__errno_r(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoll.c b/newlib/libc/stdlib/wcstoll.c
index c0e5dc747..79f4d8f94 100644
--- a/newlib/libc/stdlib/wcstoll.c
+++ b/newlib/libc/stdlib/wcstoll.c
@@ -200,7 +200,7 @@ _wcstoll_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_LONG_MIN : LONG_LONG_MAX;
-		rptr->_errno = ERANGE;
+		__errno_r(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoul.c b/newlib/libc/stdlib/wcstoul.c
index fe3c87867..27f6f52b6 100644
--- a/newlib/libc/stdlib/wcstoul.c
+++ b/newlib/libc/stdlib/wcstoul.c
@@ -179,7 +179,7 @@ _wcstoul_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = ULONG_MAX;
-		rptr->_errno = ERANGE;
+		__errno_r(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoull.c b/newlib/libc/stdlib/wcstoull.c
index 5ac325790..4f02c7482 100644
--- a/newlib/libc/stdlib/wcstoull.c
+++ b/newlib/libc/stdlib/wcstoull.c
@@ -150,7 +150,7 @@ _wcstoull_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	register int neg = 0, any, cutlim;
 
 	if(base < 0  ||  base == 1  ||  base > 36)  {
-		rptr->_errno = EINVAL;
+		__errno_r(rptr) = EINVAL;
 		return(0ULL);
 	}
 	/*
@@ -195,7 +195,7 @@ _wcstoull_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = ULLONG_MAX;
-		rptr->_errno = ERANGE;
+		__errno_r(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoumax.c b/newlib/libc/stdlib/wcstoumax.c
index 17b5275fa..d0a4f108b 100644
--- a/newlib/libc/stdlib/wcstoumax.c
+++ b/newlib/libc/stdlib/wcstoumax.c
@@ -121,10 +121,10 @@ _wcstoumax_l(struct _reent *rptr,const wchar_t * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = UINTMAX_MAX;
-		rptr->_errno = ERANGE;
+		__errno_r(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		__errno_r(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/wctomb_r.c b/newlib/libc/stdlib/wctomb_r.c
index b4799341e..059b7703f 100644
--- a/newlib/libc/stdlib/wctomb_r.c
+++ b/newlib/libc/stdlib/wctomb_r.c
@@ -35,7 +35,7 @@ __ascii_wctomb (struct _reent *r,
   if ((size_t)wchar >= 0x100)
 #endif
     {
-      r->_errno = EILSEQ;
+      __errno_r(r) = EILSEQ;
       return -1;
     }
 
@@ -133,7 +133,7 @@ __utf8_wctomb (struct _reent *r,
       return 4;
     }
 
-  r->_errno = EILSEQ;
+  __errno_r(r) = EILSEQ;
   return -1;
 }
 
@@ -165,7 +165,7 @@ __sjis_wctomb (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -204,7 +204,7 @@ __eucjp_wctomb (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -244,7 +244,7 @@ __jis_wctomb (struct _reent *r,
 	  *s = (char)char2;
 	  return cnt + 2;
 	}
-      r->_errno = EILSEQ;
+      __errno_r(r) = EILSEQ;
       return -1;
     }
   if (state->__state != 0)
@@ -284,14 +284,14 @@ ___iso_wctomb (struct _reent *r, char *s, wchar_t _wchar, int iso_idx,
 		*s = (char) (mb + 0xa0);
 		return 1;
 	      }
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
  
   if ((size_t)wchar >= 0x100)
     {
-      r->_errno = EILSEQ;
+      __errno_r(r) = EILSEQ;
       return -1;
     }
 
@@ -440,14 +440,14 @@ ___cp_wctomb (struct _reent *r, char *s, wchar_t _wchar, int cp_idx,
 		*s = (char) (mb + 0x80);
 		return 1;
 	      }
-	  r->_errno = EILSEQ;
+	  __errno_r(r) = EILSEQ;
 	  return -1;
 	}
     }
 
   if ((size_t)wchar >= 0x100)
     {
-      r->_errno = EILSEQ;
+      __errno_r(r) = EILSEQ;
       return -1;
     }
 
diff --git a/newlib/libc/string/strerror.c b/newlib/libc/string/strerror.c
index c1b934696..5bed5ffbe 100644
--- a/newlib/libc/string/strerror.c
+++ b/newlib/libc/string/strerror.c
@@ -882,7 +882,7 @@ _strerror_r (struct _reent *ptr,
 #endif
     default:
       if (!errptr)
-        errptr = &ptr->_errno;
+        errptr = &__errno_r(ptr);
       if ((error = _user_strerror (errnum, internal, errptr)) == 0)
         error = "";
       break;
diff --git a/winsup/cygwin/cygerrno.h b/winsup/cygwin/cygerrno.h
index 940bfa5e0..04436d28a 100644
--- a/winsup/cygwin/cygerrno.h
+++ b/winsup/cygwin/cygerrno.h
@@ -36,7 +36,7 @@ extern inline int
 __set_errno (const char *fn, int ln, int val)
 {
   debug_printf ("%s:%d setting errno %d", fn, ln, val);
-  return errno = _impure_ptr->_errno = val;
+  return errno = __errno_r(_impure_ptr) = val;
 }
 #define set_errno(val) __set_errno (__PRETTY_FUNCTION__, __LINE__, (val))
 
@@ -55,7 +55,7 @@ class save_errno
     save_errno (int what) {saved = get_errno (); set_errno (what); }
     void set (int what) {set_errno (what); saved = what;}
     void reset () {saved = get_errno ();}
-    ~save_errno () {errno = _impure_ptr->_errno = saved;}
+    ~save_errno () {errno = __errno_r(_impure_ptr) = saved;}
   };
 
 extern const char *__sp_fn;
diff --git a/winsup/cygwin/errno.cc b/winsup/cygwin/errno.cc
index f200e5b73..6ad0e6d78 100644
--- a/winsup/cygwin/errno.cc
+++ b/winsup/cygwin/errno.cc
@@ -335,7 +335,7 @@ void
 seterrno_from_win_error (const char *file, int line, DWORD code)
 {
   syscall_printf ("%s:%d windows error %u", file, line, code);
-  errno = _impure_ptr->_errno =  geterrno_from_win_error (code, EACCES);
+  errno = __errno_r(_impure_ptr) =  geterrno_from_win_error (code, EACCES);
 }
 
 int
@@ -353,7 +353,7 @@ seterrno_from_nt_status (const char *file, int line, NTSTATUS status)
   SetLastError (code);
   syscall_printf ("%s:%d status %y -> windows error %u",
 		  file, line, status, code);
-  errno = _impure_ptr->_errno =  geterrno_from_win_error (code, EACCES);
+  errno = __errno_r(_impure_ptr) =  geterrno_from_win_error (code, EACCES);
 }
 
 static char *
diff --git a/winsup/cygwin/strfuncs.cc b/winsup/cygwin/strfuncs.cc
index 9a97b3a24..3521c0764 100644
--- a/winsup/cygwin/strfuncs.cc
+++ b/winsup/cygwin/strfuncs.cc
@@ -144,7 +144,7 @@ __db_wctomb (struct _reent *r, char *s, wchar_t wchar, UINT cp)
   if (ret > 0 && !def_used)
     return ret;
 
-  r->_errno = EILSEQ;
+  __errno_r(r) = EILSEQ;
   return -1;
 }
 
@@ -194,7 +194,7 @@ __eucjp_wctomb (struct _reent *r, char *s, wchar_t wchar, mbstate_t *state)
       return ret;
     }
 
-  r->_errno = EILSEQ;
+  __errno_r(r) = EILSEQ;
   return -1;
 }
 
@@ -255,7 +255,7 @@ __db_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n, UINT cp,
 	 here is to check if the first byte returns a valid value... */
       else if (MultiByteToWideChar (cp, MB_ERR_INVALID_CHARS, s, 1, pwc, 1))
 	return 1;
-      r->_errno = EILSEQ;
+      __errno_r(r) = EILSEQ;
       return -1;
     }
   state->__value.__wchb[state->__count] = *s;
@@ -263,7 +263,7 @@ __db_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n, UINT cp,
 			     (const char *) state->__value.__wchb, 2, pwc, 1);
   if (!ret)
     {
-      r->_errno = EILSEQ;
+      __errno_r(r) = EILSEQ;
       return -1;
     }
   state->__count = 0;
@@ -324,7 +324,7 @@ __eucjp_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
 	}
       else if (MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS, s, 1, pwc, 1))
 	return 1;
-      r->_errno = EILSEQ;
+      __errno_r(r) = EILSEQ;
       return -1;
     }
   state->__value.__wchb[state->__count++] = *s;
@@ -347,7 +347,7 @@ jis_x_0212:
   if (!MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS,
 			    (const char *) state->__value.__wchb, 2, pwc, 1))
     {
-      r->_errno = EILSEQ;
+      __errno_r(r) = EILSEQ;
       return -1;
     }
   state->__count = 0;
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index 7d53c2af7..a4525f2e0 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -1900,7 +1900,7 @@ _fstat_r (struct _reent *ptr, int fd, struct stat *buf)
   int ret;
 
   if ((ret = fstat (fd, buf)) == -1)
-    ptr->_errno = get_errno ();
+    __errno_r(ptr) = get_errno ();
   return ret;
 }
 
@@ -2051,7 +2051,7 @@ _stat_r (struct _reent *__restrict ptr, const char *__restrict name,
   int ret;
 
   if ((ret = stat (name, buf)) == -1)
-    ptr->_errno = get_errno ();
+    __errno_r(ptr) = get_errno ();
   return ret;
 }
 
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH 00/14] Add --enable-newlib-reent-thread-local option
  2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
                   ` (13 preceding siblings ...)
  2022-06-21 12:49 ` [PATCH 14/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
@ 2022-07-12 11:18 ` Sebastian Huber
  2022-07-12 14:45   ` Corinna Vinschen
  14 siblings, 1 reply; 30+ messages in thread
From: Sebastian Huber @ 2022-07-12 11:18 UTC (permalink / raw)
  To: newlib

On 21/06/2022 14:49, Sebastian Huber wrote:
> By default, Newlib uses a huge object of type struct _reent to store
> thread-specific data.  This object is returned by __getreent() if the
> __DYNAMIC_REENT__ Newlib configuration option is defined.
> 
> The reentrancy structure contains for example errno and the standard input,
> output, and error file streams.  This means that if an application only uses
> errno it has a dependency on the file stream support even if it does not use
> it.  This is an issue for lower end targets and applications which need to
> qualify the software according to safety standards (for example ECSS-E-ST-40C,
> ECSS-Q-ST-80C, IEC 61508, ISO 26262, DO-178, DO-330, DO-333).
> 
> If the new _REENT_THREAD_LOCAL configuration option is enabled, then struct
> _reent is replaced by dedicated thread-local objects for each struct _reent
> member.  The thread-local objects are defined in translation units which use
> the corresponding object.
> 
> Patches 1 to 13 contain no functional changes (hopefully).  They just introduce
> macros to be able to use struct _reent members or thread-local objects
> depending on the new _REENT_THREAD_LOCAL option.  Patch 14 adds the new
> configuration option.

Are there any general objections to integrate this patch set?

For the errno handling we have the option to use the new _REENT_ERRNO() 
macro or the existing __errno_r() macro.

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH 00/14] Add --enable-newlib-reent-thread-local option
  2022-07-12 11:18 ` [PATCH 00/14] " Sebastian Huber
@ 2022-07-12 14:45   ` Corinna Vinschen
  2022-07-12 15:53     ` Sebastian Huber
  0 siblings, 1 reply; 30+ messages in thread
From: Corinna Vinschen @ 2022-07-12 14:45 UTC (permalink / raw)
  To: newlib

On Jul 12 13:18, Sebastian Huber wrote:
> On 21/06/2022 14:49, Sebastian Huber wrote:
> > By default, Newlib uses a huge object of type struct _reent to store
> > thread-specific data.  This object is returned by __getreent() if the
> > __DYNAMIC_REENT__ Newlib configuration option is defined.
> > 
> > The reentrancy structure contains for example errno and the standard input,
> > output, and error file streams.  This means that if an application only uses
> > errno it has a dependency on the file stream support even if it does not use
> > it.  This is an issue for lower end targets and applications which need to
> > qualify the software according to safety standards (for example ECSS-E-ST-40C,
> > ECSS-Q-ST-80C, IEC 61508, ISO 26262, DO-178, DO-330, DO-333).
> > 
> > If the new _REENT_THREAD_LOCAL configuration option is enabled, then struct
> > _reent is replaced by dedicated thread-local objects for each struct _reent
> > member.  The thread-local objects are defined in translation units which use
> > the corresponding object.
> > 
> > Patches 1 to 13 contain no functional changes (hopefully).  They just introduce
> > macros to be able to use struct _reent members or thread-local objects
> > depending on the new _REENT_THREAD_LOCAL option.  Patch 14 adds the new
> > configuration option.
> 
> Are there any general objections to integrate this patch set?

Only that neither Jeff nor I reviewed the patchset yet.


Corinna


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH 00/14] Add --enable-newlib-reent-thread-local option
  2022-07-12 14:45   ` Corinna Vinschen
@ 2022-07-12 15:53     ` Sebastian Huber
  0 siblings, 0 replies; 30+ messages in thread
From: Sebastian Huber @ 2022-07-12 15:53 UTC (permalink / raw)
  To: newlib

On 12/07/2022 16:45, Corinna Vinschen wrote:
> On Jul 12 13:18, Sebastian Huber wrote:
>> Are there any general objections to integrate this patch set?
> Only that neither Jeff nor I reviewed the patchset yet.

Ok, good. I was a bit unsure since other patch sets posted later were 
already reviewed.

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Fw: [PATCH 03/14] Add _REENT_ERRNO(ptr)
  2022-06-23 10:55       ` Sebastian Huber
  2022-07-11  7:41         ` Sebastian Huber
@ 2022-07-12 16:11         ` Corinna Vinschen
  2022-07-12 16:38           ` Sebastian Huber
  1 sibling, 1 reply; 30+ messages in thread
From: Corinna Vinschen @ 2022-07-12 16:11 UTC (permalink / raw)
  To: newlib

On Jun 23 12:55, Sebastian Huber wrote:
> On 21/06/2022 16:41, C Howland wrote:
> > > 
> > > ------------------------------
> > > *From:* Newlib<newlib-bounces+craig.howland=caci.com@sourceware.org>  on
> > > behalf of Sebastian Huber<sebastian.huber@embedded-brains.de>
> > > *Sent:* Tuesday, June 21, 2022 8:49 AM
> > > *To:*newlib@sourceware.org  <newlib@sourceware.org>
> > > *Subject:* [PATCH 03/14] Add _REENT_ERRNO(ptr)
> > > 
> > > 
> > > 
> > > From: Matt Joyce<matthew.joyce@embedded-brains.de>
> > > 
> > > Add a _REENT_ERRNO() macro to encapsulate the access to the
> > > _errno member of struct reent. This will help to replace the
> > > structure member with a thread-local storage object in a follow
> > > up patch.
> > > ---
> > > 
> > There already exists an __errno_r() macro that does the very same function
> > (defined in sys/errno.h).  (Its use, however, is limited, only being used
> > in files under iconv/lib.)  Having the same thing done both ways probably
> > doesn't make sense.  The new name is more consistent with the rest of the
> > things being done, while the old name is established and errno is a more
> > specialized case.  It probably would be a good idea to either
> > 1)  use __errno_r() instead of creating _REENT_ERRNO() or
> > 2)  replace __errno_r() with _REENT_ERRNO() as part of adding the latter.
> 
> I would not remove an existing macro, so option 1) would be preferred by me.

Really?  Your followup patches introduce a lot of new _REENT_foo macros,
so defining one of them with a different name doesn't make a lot of sense,
does it?

Either all these macros should be called __foo_r(), or __errno_r() should
actually be removed or at least be defined in terms of _REENT_errno(),
if you really think we should keep it.  Given that it's used only in
iconv/lib kind of shows that it was never meant for consumption outside
newlib anyway, isn't it?

Jeff?


Corinna


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Fw: [PATCH 03/14] Add _REENT_ERRNO(ptr)
  2022-07-12 16:11         ` Corinna Vinschen
@ 2022-07-12 16:38           ` Sebastian Huber
  2022-07-12 18:25             ` Corinna Vinschen
  0 siblings, 1 reply; 30+ messages in thread
From: Sebastian Huber @ 2022-07-12 16:38 UTC (permalink / raw)
  To: newlib

On 12/07/2022 18:11, Corinna Vinschen wrote:
> On Jun 23 12:55, Sebastian Huber wrote:
>> On 21/06/2022 16:41, C Howland wrote:
>>>> ------------------------------
>>>> *From:* Newlib<newlib-bounces+craig.howland=caci.com@sourceware.org>   on
>>>> behalf of Sebastian Huber<sebastian.huber@embedded-brains.de>
>>>> *Sent:* Tuesday, June 21, 2022 8:49 AM
>>>> *To:*newlib@sourceware.org<newlib@sourceware.org>
>>>> *Subject:* [PATCH 03/14] Add _REENT_ERRNO(ptr)
>>>>
>>>>
>>>>
>>>> From: Matt Joyce<matthew.joyce@embedded-brains.de>
>>>>
>>>> Add a _REENT_ERRNO() macro to encapsulate the access to the
>>>> _errno member of struct reent. This will help to replace the
>>>> structure member with a thread-local storage object in a follow
>>>> up patch.
>>>> ---
>>>>
>>> There already exists an __errno_r() macro that does the very same function
>>> (defined in sys/errno.h).  (Its use, however, is limited, only being used
>>> in files under iconv/lib.)  Having the same thing done both ways probably
>>> doesn't make sense.  The new name is more consistent with the rest of the
>>> things being done, while the old name is established and errno is a more
>>> specialized case.  It probably would be a good idea to either
>>> 1)  use __errno_r() instead of creating _REENT_ERRNO() or
>>> 2)  replace __errno_r() with _REENT_ERRNO() as part of adding the latter.
>> I would not remove an existing macro, so option 1) would be preferred by me.
> Really?  Your followup patches introduce a lot of new _REENT_foo macros,
> so defining one of them with a different name doesn't make a lot of sense,
> does it?

There are lots of *_r() functions (for example _strdup_r()) so I assumed 
that this __errno_r() might be used outside of Newlib.

> 
> Either all these macros should be called __foo_r(), or __errno_r() should
> actually be removed or at least be defined in terms of _REENT_errno(),
> if you really think we should keep it.  

The original patch defined __errno_r() like this:

#define __errno_r(ptr) _REENT_ERRNO(ptr)

I think naming of the new _REENT_*() macros is quite consistent with the 
stuff we already have in <sys/reent.h>.

> Given that it's used only in
> iconv/lib kind of shows that it was never meant for consumption outside
> newlib anyway, isn't it?

What about renaming the existing __errno_r() uses in Newlib to 
_REENT_ERRNO() with a definition of __errno_r() in <sys/errno.h> for 
potential users outside of Newlib?

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Fw: [PATCH 03/14] Add _REENT_ERRNO(ptr)
  2022-07-12 16:38           ` Sebastian Huber
@ 2022-07-12 18:25             ` Corinna Vinschen
  2022-07-13  7:17               ` Sebastian Huber
  0 siblings, 1 reply; 30+ messages in thread
From: Corinna Vinschen @ 2022-07-12 18:25 UTC (permalink / raw)
  To: newlib

On Jul 12 18:38, Sebastian Huber wrote:
> On 12/07/2022 18:11, Corinna Vinschen wrote:
> > On Jun 23 12:55, Sebastian Huber wrote:
> > > On 21/06/2022 16:41, C Howland wrote:
> > > > The new name is more consistent with the rest of the
> > > > things being done, while the old name is established and errno is a more
> > > > specialized case.  It probably would be a good idea to either
> > > > 1)  use __errno_r() instead of creating _REENT_ERRNO() or
> > > > 2)  replace __errno_r() with _REENT_ERRNO() as part of adding the latter.
> > > I would not remove an existing macro, so option 1) would be preferred by me.
> > Really?  Your followup patches introduce a lot of new _REENT_foo macros,
> > so defining one of them with a different name doesn't make a lot of sense,
> > does it?
> 
> There are lots of *_r() functions (for example _strdup_r()) so I assumed
> that this __errno_r() might be used outside of Newlib.
> 
> > 
> > Either all these macros should be called __foo_r(), or __errno_r() should
> > actually be removed or at least be defined in terms of _REENT_errno(),
> > if you really think we should keep it.
> 
> The original patch defined __errno_r() like this:
> 
> #define __errno_r(ptr) _REENT_ERRNO(ptr)
> 
> I think naming of the new _REENT_*() macros is quite consistent with the
> stuff we already have in <sys/reent.h>.
> 
> > Given that it's used only in
> > iconv/lib kind of shows that it was never meant for consumption outside
> > newlib anyway, isn't it?
> 
> What about renaming the existing __errno_r() uses in Newlib to
> _REENT_ERRNO() with a definition of __errno_r() in <sys/errno.h> for
> potential users outside of Newlib?

Sounds good to me.


Corinna


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Fw: [PATCH 03/14] Add _REENT_ERRNO(ptr)
  2022-07-12 18:25             ` Corinna Vinschen
@ 2022-07-13  7:17               ` Sebastian Huber
  2022-07-13  7:50                 ` Corinna Vinschen
  0 siblings, 1 reply; 30+ messages in thread
From: Sebastian Huber @ 2022-07-13  7:17 UTC (permalink / raw)
  To: newlib

[-- Attachment #1: Type: text/plain, Size: 801 bytes --]

On 12/07/2022 20:25, Corinna Vinschen wrote:
>> What about renaming the existing __errno_r() uses in Newlib to
>> _REENT_ERRNO() with a definition of __errno_r() in <sys/errno.h> for
>> potential users outside of Newlib?
> Sounds good to me.

Attached is a new version of this patch. In this patch the uses of 
__errno_r() were replaced with _REENT_ERRNO().

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/

[-- Attachment #2: 0001-Add-_REENT_ERRNO-ptr.patch --]
[-- Type: text/x-patch, Size: 84140 bytes --]

From f3b8138239d3ba34c4ecaa4305b0fbd7eb4e28a5 Mon Sep 17 00:00:00 2001
From: Matt Joyce <matthew.joyce@embedded-brains.de>
Date: Tue, 18 Jan 2022 10:13:04 +0100
Subject: [PATCH] Add _REENT_ERRNO(ptr)

Add a _REENT_ERRNO() macro to encapsulate the access to the
_errno member of struct reent. This will help to replace the
structure member with a thread-local storage object in a follow
up patch.

Replace uses of __errno_r() with _REENT_ERRNO().  Keep __errno_r() macro for
potential users outside of Newlib.
---
 newlib/libc/ctype/wctrans.c               |  2 +-
 newlib/libc/ctype/wctype.c                |  2 +-
 newlib/libc/errno/errno.c                 |  2 +-
 newlib/libc/iconv/lib/iconv.c             | 10 +++----
 newlib/libc/iconv/lib/iconvnls.c          |  4 +--
 newlib/libc/iconv/lib/nullconv.c          |  2 +-
 newlib/libc/iconv/lib/ucsconv.c           | 12 ++++----
 newlib/libc/include/sys/errno.h           |  2 +-
 newlib/libc/include/sys/reent.h           |  1 +
 newlib/libc/machine/cris/sys/errno.h      |  2 +-
 newlib/libc/machine/powerpc/strtosfix16.c | 10 +++----
 newlib/libc/machine/powerpc/strtosfix32.c | 10 +++----
 newlib/libc/machine/powerpc/strtosfix64.c | 10 +++----
 newlib/libc/machine/powerpc/strtoufix16.c | 10 +++----
 newlib/libc/machine/powerpc/strtoufix32.c | 10 +++----
 newlib/libc/machine/powerpc/strtoufix64.c | 10 +++----
 newlib/libc/machine/spu/stdio.c           |  2 +-
 newlib/libc/machine/spu/sys/errno.h       |  2 +-
 newlib/libc/reent/closer.c                |  2 +-
 newlib/libc/reent/execr.c                 |  6 ++--
 newlib/libc/reent/fcntlr.c                |  2 +-
 newlib/libc/reent/fstat64r.c              |  2 +-
 newlib/libc/reent/fstatr.c                |  2 +-
 newlib/libc/reent/gettimeofdayr.c         |  2 +-
 newlib/libc/reent/isattyr.c               |  2 +-
 newlib/libc/reent/linkr.c                 |  2 +-
 newlib/libc/reent/lseek64r.c              |  2 +-
 newlib/libc/reent/lseekr.c                |  2 +-
 newlib/libc/reent/mkdirr.c                |  2 +-
 newlib/libc/reent/open64r.c               |  2 +-
 newlib/libc/reent/openr.c                 |  2 +-
 newlib/libc/reent/readr.c                 |  2 +-
 newlib/libc/reent/renamer.c               |  2 +-
 newlib/libc/reent/sbrkr.c                 |  2 +-
 newlib/libc/reent/signalr.c               |  2 +-
 newlib/libc/reent/stat64r.c               |  2 +-
 newlib/libc/reent/statr.c                 |  2 +-
 newlib/libc/reent/unlinkr.c               |  2 +-
 newlib/libc/reent/writer.c                |  2 +-
 newlib/libc/signal/signal.c               |  6 ++--
 newlib/libc/stdio/asniprintf.c            |  4 +--
 newlib/libc/stdio/asnprintf.c             |  4 +--
 newlib/libc/stdio/fdopen.c                |  2 +-
 newlib/libc/stdio/fflush.c                | 18 ++++++------
 newlib/libc/stdio/fileno.c                |  2 +-
 newlib/libc/stdio/fileno_u.c              |  2 +-
 newlib/libc/stdio/findfp.c                |  2 +-
 newlib/libc/stdio/flags.c                 |  2 +-
 newlib/libc/stdio/fmemopen.c              | 14 +++++-----
 newlib/libc/stdio/fopencookie.c           | 14 +++++-----
 newlib/libc/stdio/fpurge.c                |  2 +-
 newlib/libc/stdio/freopen.c               |  4 +--
 newlib/libc/stdio/fseeko.c                |  4 +--
 newlib/libc/stdio/ftell.c                 |  2 +-
 newlib/libc/stdio/ftello.c                |  2 +-
 newlib/libc/stdio/funopen.c               | 16 +++++------
 newlib/libc/stdio/fvwrite.c               |  4 +--
 newlib/libc/stdio/mktemp.c                | 14 +++++-----
 newlib/libc/stdio/nano-vfprintf.c         | 10 +++----
 newlib/libc/stdio/open_memstream.c        | 14 +++++-----
 newlib/libc/stdio/perror.c                |  2 +-
 newlib/libc/stdio/refill.c                |  2 +-
 newlib/libc/stdio/sniprintf.c             |  8 +++---
 newlib/libc/stdio/snprintf.c              |  8 +++---
 newlib/libc/stdio/swprintf.c              |  8 +++---
 newlib/libc/stdio/tmpfile.c               |  6 ++--
 newlib/libc/stdio/tmpnam.c                |  2 +-
 newlib/libc/stdio/vasniprintf.c           |  2 +-
 newlib/libc/stdio/vasnprintf.c            |  2 +-
 newlib/libc/stdio/vfprintf.c              | 12 ++++----
 newlib/libc/stdio/vfscanf.c               |  2 +-
 newlib/libc/stdio/vfwprintf.c             |  4 +--
 newlib/libc/stdio/vfwscanf.c              |  2 +-
 newlib/libc/stdio/vsniprintf.c            |  4 +--
 newlib/libc/stdio/vsnprintf.c             |  4 +--
 newlib/libc/stdio/vswprintf.c             |  4 +--
 newlib/libc/stdio/wsetup.c                |  2 +-
 newlib/libc/stdio64/fdopen64.c            |  2 +-
 newlib/libc/stdio64/freopen64.c           |  4 +--
 newlib/libc/stdio64/fseeko64.c            |  6 ++--
 newlib/libc/stdio64/ftello64.c            |  2 +-
 newlib/libc/stdio64/tmpfile64.c           |  6 ++--
 newlib/libc/stdlib/__adjust.c             |  4 +--
 newlib/libc/stdlib/mbrtowc.c              |  4 +--
 newlib/libc/stdlib/mbsnrtowcs.c           |  2 +-
 newlib/libc/stdlib/mbtowc_r.c             | 34 +++++++++++------------
 newlib/libc/stdlib/strtod.c               | 10 +++----
 newlib/libc/stdlib/strtoimax.c            |  4 +--
 newlib/libc/stdlib/strtol.c               |  2 +-
 newlib/libc/stdlib/strtoll.c              |  2 +-
 newlib/libc/stdlib/strtoul.c              |  2 +-
 newlib/libc/stdlib/strtoull.c             |  2 +-
 newlib/libc/stdlib/strtoumax.c            |  4 +--
 newlib/libc/stdlib/wcrtomb.c              |  4 +--
 newlib/libc/stdlib/wcsnrtombs.c           |  2 +-
 newlib/libc/stdlib/wcstod.c               |  4 +--
 newlib/libc/stdlib/wcstoimax.c            |  4 +--
 newlib/libc/stdlib/wcstol.c               |  2 +-
 newlib/libc/stdlib/wcstoll.c              |  2 +-
 newlib/libc/stdlib/wcstoul.c              |  2 +-
 newlib/libc/stdlib/wcstoull.c             |  4 +--
 newlib/libc/stdlib/wcstoumax.c            |  4 +--
 newlib/libc/stdlib/wctomb_r.c             | 18 ++++++------
 newlib/libc/string/strerror.c             |  2 +-
 winsup/cygwin/cygerrno.h                  |  4 +--
 winsup/cygwin/errno.cc                    |  4 +--
 winsup/cygwin/strfuncs.cc                 | 12 ++++----
 winsup/cygwin/syscalls.cc                 |  4 +--
 108 files changed, 264 insertions(+), 263 deletions(-)

diff --git a/newlib/libc/ctype/wctrans.c b/newlib/libc/ctype/wctrans.c
index 4a58df152..dbb103d38 100644
--- a/newlib/libc/ctype/wctrans.c
+++ b/newlib/libc/ctype/wctrans.c
@@ -84,7 +84,7 @@ _wctrans_r (struct _reent *r,
     return WCT_TOUPPER;
   else
     {
-      r->_errno = EINVAL;
+      _REENT_ERRNO(r) = EINVAL;
       return 0;
     }
 }
diff --git a/newlib/libc/ctype/wctype.c b/newlib/libc/ctype/wctype.c
index 5f2ae7a25..8c7afc731 100644
--- a/newlib/libc/ctype/wctype.c
+++ b/newlib/libc/ctype/wctype.c
@@ -128,7 +128,7 @@ _wctype_r (struct _reent *r,
     }
 
   /* otherwise invalid */
-  r->_errno = EINVAL;
+  _REENT_ERRNO(r) = EINVAL;
   return 0;
 }
 
diff --git a/newlib/libc/errno/errno.c b/newlib/libc/errno/errno.c
index fd1743d73..70402924b 100644
--- a/newlib/libc/errno/errno.c
+++ b/newlib/libc/errno/errno.c
@@ -10,7 +10,7 @@
 int *
 __errno ()
 {
-  return &_REENT->_errno;
+  return &_REENT_ERRNO(_REENT);
 }
 
 #endif
diff --git a/newlib/libc/iconv/lib/iconv.c b/newlib/libc/iconv/lib/iconv.c
index 02c749d7f..458541cb9 100644
--- a/newlib/libc/iconv/lib/iconv.c
+++ b/newlib/libc/iconv/lib/iconv.c
@@ -211,7 +211,7 @@ _iconv_r (struct _reent *rptr,
        || (ic->handlers != &_iconv_null_conversion_handlers
            && ic->handlers != &_iconv_ucs_conversion_handlers))
     {
-      __errno_r (rptr) = EBADF;
+      _REENT_ERRNO (rptr) = EBADF;
       return (size_t)-1;
     }
 
@@ -257,19 +257,19 @@ _iconv_r (struct _reent *rptr,
            ic->handlers->set_state (ic->data, &state_save, 1);
         }
        
-      __errno_r (rptr) = E2BIG;
+      _REENT_ERRNO (rptr) = E2BIG;
       return (size_t)-1;
     }
   
   if (*inbytesleft == 0)
     {
-      __errno_r (rptr) = EINVAL;
+      _REENT_ERRNO (rptr) = EINVAL;
       return (size_t)-1;
     }
    
   if (*outbytesleft == 0 || *outbuf == NULL)
     {
-      __errno_r (rptr) = E2BIG;
+      _REENT_ERRNO (rptr) = E2BIG;
       return (size_t)-1;
     }
 
@@ -294,7 +294,7 @@ _iconv_close_r (struct _reent *rptr,
        || (ic->handlers != &_iconv_null_conversion_handlers
            && ic->handlers != &_iconv_ucs_conversion_handlers))
     {
-      __errno_r (rptr) = EBADF;
+      _REENT_ERRNO (rptr) = EBADF;
       return -1;
     }
 
diff --git a/newlib/libc/iconv/lib/iconvnls.c b/newlib/libc/iconv/lib/iconvnls.c
index ed8f3a7ba..5d4b0a273 100644
--- a/newlib/libc/iconv/lib/iconvnls.c
+++ b/newlib/libc/iconv/lib/iconvnls.c
@@ -177,7 +177,7 @@ _iconv_nls_conv (struct _reent *rptr,
        || (ic->handlers != &_iconv_null_conversion_handlers
            && ic->handlers != &_iconv_ucs_conversion_handlers))
     {
-      __errno_r (rptr) = EBADF;
+      _REENT_ERRNO (rptr) = EBADF;
       return (size_t)-1;
     }
   
@@ -189,7 +189,7 @@ _iconv_nls_conv (struct _reent *rptr,
   
   if (outbytesleft == NULL || *outbytesleft == 0)
     {
-      __errno_r (rptr) = E2BIG;
+      _REENT_ERRNO (rptr) = E2BIG;
       return (size_t)-1;
     }
 
diff --git a/newlib/libc/iconv/lib/nullconv.c b/newlib/libc/iconv/lib/nullconv.c
index e75d83376..cc4229181 100644
--- a/newlib/libc/iconv/lib/nullconv.c
+++ b/newlib/libc/iconv/lib/nullconv.c
@@ -73,7 +73,7 @@ null_conversion_convert (struct _reent *rptr,
     {
       result = (size_t)-1;
       len = *outbytesleft;
-      __errno_r (rptr) = E2BIG;
+      _REENT_ERRNO (rptr) = E2BIG;
     }
   
   if ((flags & 1) == 0)
diff --git a/newlib/libc/iconv/lib/ucsconv.c b/newlib/libc/iconv/lib/ucsconv.c
index 59f4f993d..502aa440a 100644
--- a/newlib/libc/iconv/lib/ucsconv.c
+++ b/newlib/libc/iconv/lib/ucsconv.c
@@ -164,7 +164,7 @@ ucs_based_conversion_convert (struct _reent *rptr,
 
       if (*outbytesleft == 0)
         {
-          __errno_r (rptr) = E2BIG;
+          _REENT_ERRNO (rptr) = E2BIG;
           return (size_t)-1;
         }
 
@@ -173,13 +173,13 @@ ucs_based_conversion_convert (struct _reent *rptr,
 
       if (ch == (ucs4_t)ICONV_CES_BAD_SEQUENCE)
         {
-          __errno_r (rptr) = EINVAL;
+          _REENT_ERRNO (rptr) = EINVAL;
           return (size_t)-1;
         }
 
       if (ch == (ucs4_t)ICONV_CES_INVALID_CHARACTER)
         {
-          __errno_r (rptr) = EILSEQ;
+          _REENT_ERRNO (rptr) = EILSEQ;
           return (size_t)-1;
         }
 
@@ -196,7 +196,7 @@ ucs_based_conversion_convert (struct _reent *rptr,
         {
           *inbuf = inbuf_save;
           *inbytesleft = inbyteslef_save;
-          __errno_r (rptr) = E2BIG;
+          _REENT_ERRNO (rptr) = E2BIG;
           return (size_t)-1;
         }
       else if (bytes == (size_t)ICONV_CES_INVALID_CHARACTER)
@@ -204,7 +204,7 @@ ucs_based_conversion_convert (struct _reent *rptr,
           if (flags & ICONV_FAIL_BIT)
             {
               /* Generate error */
-              __errno_r (rptr) = EILSEQ;
+              _REENT_ERRNO (rptr) = EILSEQ;
               return (size_t)-1;
             }
           /*
@@ -221,7 +221,7 @@ ucs_based_conversion_convert (struct _reent *rptr,
                                          outbytesleft);
           if ((__int32_t)bytes < 0)
             {
-              __errno_r (rptr) = E2BIG;
+              _REENT_ERRNO (rptr) = E2BIG;
               return (size_t)-1;
             }
       
diff --git a/newlib/libc/include/sys/errno.h b/newlib/libc/include/sys/errno.h
index 025b461d4..995f30e05 100644
--- a/newlib/libc/include/sys/errno.h
+++ b/newlib/libc/include/sys/errno.h
@@ -26,7 +26,7 @@ extern __IMPORT char *program_invocation_name;
 extern __IMPORT char *program_invocation_short_name;
 #endif
 
-#define __errno_r(ptr) ((ptr)->_errno)
+#define __errno_r(ptr) _REENT_ERRNO(ptr)
 
 #define	EPERM 1		/* Not owner */
 #define	ENOENT 2	/* No such file or directory */
diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 2b0ff46b7..70a987100 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -719,6 +719,7 @@ struct _reent
 #endif /* !_REENT_SMALL */
 
 #define _REENT_EMERGENCY(_ptr)	((_ptr)->_emergency)
+#define _REENT_ERRNO(_ptr)	((_ptr)->_errno)
 
 #define _REENT_INIT_PTR(var) \
   { memset((var), 0, sizeof(*(var))); \
diff --git a/newlib/libc/machine/cris/sys/errno.h b/newlib/libc/machine/cris/sys/errno.h
index 3d573015d..c8113ed73 100644
--- a/newlib/libc/machine/cris/sys/errno.h
+++ b/newlib/libc/machine/cris/sys/errno.h
@@ -24,7 +24,7 @@ extern int *__errno (void);
 extern const char * const _sys_errlist[];
 extern int _sys_nerr;
 
-#define __errno_r(ptr) ((ptr)->_errno)
+#define __errno_r(ptr) _REENT_ERRNO(ptr)
 
 /* Adjusted to the linux asm/errno.h */
 #define	EPERM		 1	/* Operation not permitted */
diff --git a/newlib/libc/machine/powerpc/strtosfix16.c b/newlib/libc/machine/powerpc/strtosfix16.c
index 3fba45409..5475de88e 100644
--- a/newlib/libc/machine/powerpc/strtosfix16.c
+++ b/newlib/libc/machine/powerpc/strtosfix16.c
@@ -108,10 +108,10 @@ _strtosfix16_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return SHRT_MIN;
       return SHRT_MAX;
@@ -120,12 +120,12 @@ _strtosfix16_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return SHRT_MAX;
     }
   else if (dbl.d < -1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return SHRT_MIN;
     }
 
@@ -152,7 +152,7 @@ _strtosfix16_r (struct _reent *rptr,
       /* check if positive saturation has occurred because of rounding */
       if (!sign && result < 0)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return SHRT_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtosfix32.c b/newlib/libc/machine/powerpc/strtosfix32.c
index b21de9ea1..6941be5d5 100644
--- a/newlib/libc/machine/powerpc/strtosfix32.c
+++ b/newlib/libc/machine/powerpc/strtosfix32.c
@@ -29,10 +29,10 @@ _strtosfix32_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return LONG_MIN;
       return LONG_MAX;
@@ -41,12 +41,12 @@ _strtosfix32_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return LONG_MAX;
     }
   else if (dbl.d < -1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return LONG_MIN;
     }
 
@@ -75,7 +75,7 @@ _strtosfix32_r (struct _reent *rptr,
       /* check if positive saturation has occurred because of rounding */
       if (!sign && result < 0)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return LONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtosfix64.c b/newlib/libc/machine/powerpc/strtosfix64.c
index 16032128f..9da3ffaee 100644
--- a/newlib/libc/machine/powerpc/strtosfix64.c
+++ b/newlib/libc/machine/powerpc/strtosfix64.c
@@ -32,10 +32,10 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (ld_type == 1)
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(ldbl) & Sign_bit)
 	return LONG_LONG_MIN;
       return LONG_LONG_MAX;
@@ -63,7 +63,7 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (exp > 0 || (exp == 0 && tmp != 0x8000000000000000LL))
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return LONG_LONG_MIN;
 	}
     }
@@ -71,7 +71,7 @@ _strtosfix64_r (struct _reent *rptr,
     {
       if (exp >= 0)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return LONG_LONG_MAX;
 	}
     }
@@ -88,7 +88,7 @@ _strtosfix64_r (struct _reent *rptr,
       /* check if positive saturation has occurred because of rounding */
       if (!sign && result < 0)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return LONG_LONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtoufix16.c b/newlib/libc/machine/powerpc/strtoufix16.c
index 9b16268bb..415652dd2 100644
--- a/newlib/libc/machine/powerpc/strtoufix16.c
+++ b/newlib/libc/machine/powerpc/strtoufix16.c
@@ -106,10 +106,10 @@ _strtoufix16_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return 0;
       return USHRT_MAX;
@@ -118,12 +118,12 @@ _strtoufix16_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return USHRT_MAX;
     }
   else if (dbl.d < 0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return 0;
     }
 
@@ -147,7 +147,7 @@ _strtoufix16_r (struct _reent *rptr,
       if (negexp == 0)
 	{
 	  /* we have overflow which means saturation */
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return USHRT_MAX;
 	}
       result |= (1 << (16 - negexp));
diff --git a/newlib/libc/machine/powerpc/strtoufix32.c b/newlib/libc/machine/powerpc/strtoufix32.c
index 7d03f8c9b..4340c711f 100644
--- a/newlib/libc/machine/powerpc/strtoufix32.c
+++ b/newlib/libc/machine/powerpc/strtoufix32.c
@@ -28,10 +28,10 @@ _strtoufix32_r (struct _reent *rptr,
     {
       if (isnan (dbl.d))
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(dbl) & Sign_bit)
 	return 0;
       return ULONG_MAX;
@@ -40,12 +40,12 @@ _strtoufix32_r (struct _reent *rptr,
   /* check for normal saturation */
   if (dbl.d >= 1.0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return ULONG_MAX;
     }
   else if (dbl.d < 0)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return 0;
     }
 
@@ -77,7 +77,7 @@ _strtoufix32_r (struct _reent *rptr,
       /* if rounding causes carry, then saturation has occurred */
       if (result < tmp)
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return ULONG_MAX;
 	}
     }
diff --git a/newlib/libc/machine/powerpc/strtoufix64.c b/newlib/libc/machine/powerpc/strtoufix64.c
index a2f0484b3..205706856 100644
--- a/newlib/libc/machine/powerpc/strtoufix64.c
+++ b/newlib/libc/machine/powerpc/strtoufix64.c
@@ -31,10 +31,10 @@ _strtoufix64_r (struct _reent *rptr,
     {
       if (ld_type == 1)
 	{
-	  rptr->_errno = EDOM;
+	  _REENT_ERRNO(rptr) = EDOM;
 	  return 0;
 	}
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       if (word0(ldbl) & Sign_bit)
 	return 0;
       return ULONG_LONG_MAX;
@@ -60,14 +60,14 @@ _strtoufix64_r (struct _reent *rptr,
   /* check for saturation */
   if (sign)
     {
-      rptr->_errno = ERANGE;
+      _REENT_ERRNO(rptr) = ERANGE;
       return 0;
     }
   else
     {
       if (exp > 0 || (exp == 0 && tmp >= 0x8000000000000000LL))
 	{
-	  rptr->_errno = ERANGE;
+	  _REENT_ERRNO(rptr) = ERANGE;
 	  return ULONG_LONG_MAX;
 	}
     }
@@ -89,7 +89,7 @@ _strtoufix64_r (struct _reent *rptr,
 	  /* if rounding causes carry, then saturation has occurred */
 	  if (result < tmp)
 	    {
-	      rptr->_errno = ERANGE;
+	      _REENT_ERRNO(rptr) = ERANGE;
 	      return ULONG_LONG_MAX;
 	    }
 	}
diff --git a/newlib/libc/machine/spu/stdio.c b/newlib/libc/machine/spu/stdio.c
index 87e4c406d..1dcc88b60 100644
--- a/newlib/libc/machine/spu/stdio.c
+++ b/newlib/libc/machine/spu/stdio.c
@@ -47,7 +47,7 @@ __sfp (struct _reent *d)
       return &__fp[i];
     }
   }
-  d->_errno = EMFILE;
+  _REENT_ERRNO(d) = EMFILE;
   return NULL;
 }
 
diff --git a/newlib/libc/machine/spu/sys/errno.h b/newlib/libc/machine/spu/sys/errno.h
index b008ff6f4..8758283eb 100644
--- a/newlib/libc/machine/spu/sys/errno.h
+++ b/newlib/libc/machine/spu/sys/errno.h
@@ -34,7 +34,7 @@ extern "C" {
 extern const char * const _sys_errlist[];
 extern int _sys_nerr;
 
-#define __errno_r(ptr) ((ptr)->_errno)
+#define __errno_r(ptr) _REENT_ERRNO(ptr)
 
 /* Adjusted to the linux asm/errno.h */
 #define	EPERM		 1	/* Operation not permitted */
diff --git a/newlib/libc/reent/closer.c b/newlib/libc/reent/closer.c
index deb34b002..2d72b2ab5 100644
--- a/newlib/libc/reent/closer.c
+++ b/newlib/libc/reent/closer.c
@@ -45,7 +45,7 @@ _close_r (ptr, fd)
 
   errno = 0;
   if ((ret = _close (fd)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/execr.c b/newlib/libc/reent/execr.c
index 59b61223e..541fb8624 100644
--- a/newlib/libc/reent/execr.c
+++ b/newlib/libc/reent/execr.c
@@ -54,7 +54,7 @@ _execve_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _execve (name, argv, env)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
@@ -86,7 +86,7 @@ _fork_r (struct _reent *ptr)
 
   errno = 0;
   if ((ret = _fork ()) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
@@ -118,7 +118,7 @@ _wait_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _wait (status)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fcntlr.c b/newlib/libc/reent/fcntlr.c
index cd19d226f..f60aa091c 100644
--- a/newlib/libc/reent/fcntlr.c
+++ b/newlib/libc/reent/fcntlr.c
@@ -49,7 +49,7 @@ _fcntl_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _fcntl (fd, cmd, arg)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fstat64r.c b/newlib/libc/reent/fstat64r.c
index c546f5c1d..a049206e2 100644
--- a/newlib/libc/reent/fstat64r.c
+++ b/newlib/libc/reent/fstat64r.c
@@ -55,7 +55,7 @@ _fstat64_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _fstat64 (fd, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/fstatr.c b/newlib/libc/reent/fstatr.c
index ec906c98d..9a02e9a68 100644
--- a/newlib/libc/reent/fstatr.c
+++ b/newlib/libc/reent/fstatr.c
@@ -53,7 +53,7 @@ _fstat_r (ptr, fd, pstat)
 
   errno = 0;
   if ((ret = _fstat (fd, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/gettimeofdayr.c b/newlib/libc/reent/gettimeofdayr.c
index 9b982a993..aa60e5e3a 100644
--- a/newlib/libc/reent/gettimeofdayr.c
+++ b/newlib/libc/reent/gettimeofdayr.c
@@ -60,7 +60,7 @@ _gettimeofday_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _gettimeofday (ptimeval, ptimezone)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/isattyr.c b/newlib/libc/reent/isattyr.c
index f21bf25b2..f76945519 100644
--- a/newlib/libc/reent/isattyr.c
+++ b/newlib/libc/reent/isattyr.c
@@ -50,7 +50,7 @@ _isatty_r (ptr, fd)
 
   errno = 0;
   if ((ret = _isatty (fd)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/linkr.c b/newlib/libc/reent/linkr.c
index b22da5f94..169b6eeec 100644
--- a/newlib/libc/reent/linkr.c
+++ b/newlib/libc/reent/linkr.c
@@ -51,7 +51,7 @@ _link_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _link (old, new)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/lseek64r.c b/newlib/libc/reent/lseek64r.c
index 40769fb6d..c9afd01df 100644
--- a/newlib/libc/reent/lseek64r.c
+++ b/newlib/libc/reent/lseek64r.c
@@ -50,7 +50,7 @@ _lseek64_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _lseek64 (fd, pos, whence)) == (_off64_t) -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/lseekr.c b/newlib/libc/reent/lseekr.c
index ac2daaab9..77e66b8e1 100644
--- a/newlib/libc/reent/lseekr.c
+++ b/newlib/libc/reent/lseekr.c
@@ -47,7 +47,7 @@ _lseek_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _lseek (fd, pos, whence)) == (_off_t) -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/mkdirr.c b/newlib/libc/reent/mkdirr.c
index fd72df64c..cf66a24d3 100644
--- a/newlib/libc/reent/mkdirr.c
+++ b/newlib/libc/reent/mkdirr.c
@@ -48,7 +48,7 @@ _mkdir_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _mkdir (path, mode)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/open64r.c b/newlib/libc/reent/open64r.c
index 84bd67e34..fe288d083 100644
--- a/newlib/libc/reent/open64r.c
+++ b/newlib/libc/reent/open64r.c
@@ -52,7 +52,7 @@ _open64_r (ptr, file, flags, mode)
 
   errno = 0;
   if ((ret = _open64 (file, flags, mode)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/openr.c b/newlib/libc/reent/openr.c
index c6a7db5de..824315438 100644
--- a/newlib/libc/reent/openr.c
+++ b/newlib/libc/reent/openr.c
@@ -48,7 +48,7 @@ _open_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _open (file, flags, mode)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/readr.c b/newlib/libc/reent/readr.c
index 7fccefd32..b2ae300d3 100644
--- a/newlib/libc/reent/readr.c
+++ b/newlib/libc/reent/readr.c
@@ -47,7 +47,7 @@ _read_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = (_ssize_t)_read (fd, buf, cnt)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/renamer.c b/newlib/libc/reent/renamer.c
index 5420dc4a0..7e1e111a8 100644
--- a/newlib/libc/reent/renamer.c
+++ b/newlib/libc/reent/renamer.c
@@ -49,7 +49,7 @@ _rename_r (struct _reent *ptr,
 #ifdef HAVE_RENAME
   errno = 0;
   if ((ret = _rename (old, new)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
 #else
   if (_link_r (ptr, old, new) == -1)
     return -1;
diff --git a/newlib/libc/reent/sbrkr.c b/newlib/libc/reent/sbrkr.c
index 21c4bd913..ec948fe6b 100644
--- a/newlib/libc/reent/sbrkr.c
+++ b/newlib/libc/reent/sbrkr.c
@@ -49,7 +49,7 @@ _sbrk_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = (char *)(_sbrk (incr))) == (void *) -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/signalr.c b/newlib/libc/reent/signalr.c
index 345910e4b..863ae7400 100644
--- a/newlib/libc/reent/signalr.c
+++ b/newlib/libc/reent/signalr.c
@@ -51,7 +51,7 @@ _kill_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _kill (pid, sig)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/stat64r.c b/newlib/libc/reent/stat64r.c
index b64736ef2..160d08e33 100644
--- a/newlib/libc/reent/stat64r.c
+++ b/newlib/libc/reent/stat64r.c
@@ -53,7 +53,7 @@ _stat64_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _stat64 (file, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/statr.c b/newlib/libc/reent/statr.c
index 9388e0246..99453e789 100644
--- a/newlib/libc/reent/statr.c
+++ b/newlib/libc/reent/statr.c
@@ -53,7 +53,7 @@ _stat_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _stat (file, pstat)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/unlinkr.c b/newlib/libc/reent/unlinkr.c
index 41bac0194..495e65b02 100644
--- a/newlib/libc/reent/unlinkr.c
+++ b/newlib/libc/reent/unlinkr.c
@@ -45,7 +45,7 @@ _unlink_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = _unlink (file)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/reent/writer.c b/newlib/libc/reent/writer.c
index 704aba18b..ac2217c57 100644
--- a/newlib/libc/reent/writer.c
+++ b/newlib/libc/reent/writer.c
@@ -47,7 +47,7 @@ _write_r (struct _reent *ptr,
 
   errno = 0;
   if ((ret = (_ssize_t)_write (fd, buf, cnt)) == -1 && errno != 0)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return ret;
 }
 
diff --git a/newlib/libc/signal/signal.c b/newlib/libc/signal/signal.c
index 0110287d8..a249e9f00 100644
--- a/newlib/libc/signal/signal.c
+++ b/newlib/libc/signal/signal.c
@@ -115,7 +115,7 @@ _signal_r (struct _reent *ptr,
 
   if (sig < 0 || sig >= NSIG)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return SIG_ERR;
     }
 
@@ -136,7 +136,7 @@ _raise_r (struct _reent *ptr,
 
   if (sig < 0 || sig >= NSIG)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return -1;
     }
 
@@ -151,7 +151,7 @@ _raise_r (struct _reent *ptr,
     return 0;
   else if (func == SIG_ERR)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return 1;
     }
   else
diff --git a/newlib/libc/stdio/asniprintf.c b/newlib/libc/stdio/asniprintf.c
index 0bfe00d9b..97e77748e 100644
--- a/newlib/libc/stdio/asniprintf.c
+++ b/newlib/libc/stdio/asniprintf.c
@@ -42,7 +42,7 @@ _asniprintf_r (struct _reent *ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
@@ -88,7 +88,7 @@ asniprintf (char *buf,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/asnprintf.c b/newlib/libc/stdio/asnprintf.c
index f657f9ec9..f82556143 100644
--- a/newlib/libc/stdio/asnprintf.c
+++ b/newlib/libc/stdio/asnprintf.c
@@ -42,7 +42,7 @@ _asnprintf_r (struct _reent *__restrict ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
@@ -94,7 +94,7 @@ asnprintf (char *__restrict buf,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/fdopen.c b/newlib/libc/stdio/fdopen.c
index ef942c284..3481154d1 100644
--- a/newlib/libc/stdio/fdopen.c
+++ b/newlib/libc/stdio/fdopen.c
@@ -73,7 +73,7 @@ _fdopen_r (struct _reent *ptr,
   fdmode = fdflags & O_ACCMODE;
   if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
     {
-      ptr->_errno = EBADF;
+      _REENT_ERRNO(ptr) = EBADF;
       return 0;
     }
 #endif
diff --git a/newlib/libc/stdio/fflush.c b/newlib/libc/stdio/fflush.c
index bbec4a19b..d51e90153 100644
--- a/newlib/libc/stdio/fflush.c
+++ b/newlib/libc/stdio/fflush.c
@@ -137,8 +137,8 @@ __sflush_r (struct _reent *ptr,
 	  /* Save last errno and set errno to 0, so we can check if a device
 	     returns with a valid position -1.  We restore the last errno if
 	     no other error condition has been encountered. */
-	  tmp_errno = ptr->_errno;
-	  ptr->_errno = 0;
+	  tmp_errno = _REENT_ERRNO(ptr);
+	  _REENT_ERRNO(ptr) = 0;
 	  /* Get the physical position we are at in the file.  */
 	  if (fp->_flags & __SOFF)
 	    curoff = fp->_offset;
@@ -152,13 +152,13 @@ __sflush_r (struct _reent *ptr,
 	      else
 #endif
 		curoff = fp->_seek (ptr, fp->_cookie, 0, SEEK_CUR);
-	      if (curoff == -1L && ptr->_errno != 0)
+	      if (curoff == -1L && _REENT_ERRNO(ptr) != 0)
 		{
 		  int result = EOF;
-		  if (ptr->_errno == ESPIPE || ptr->_errno == EINVAL)
+		  if (_REENT_ERRNO(ptr) == ESPIPE || _REENT_ERRNO(ptr) == EINVAL)
 		    {
 		      result = 0;
-		      ptr->_errno = tmp_errno;
+		      _REENT_ERRNO(ptr) = tmp_errno;
 		    }
 		  else
 		    fp->_flags |= __SERR;
@@ -180,8 +180,8 @@ __sflush_r (struct _reent *ptr,
 	  else
 #endif
 	    curoff = fp->_seek (ptr, fp->_cookie, curoff, SEEK_SET);
-	  if (curoff != -1 || ptr->_errno == 0
-	      || ptr->_errno == ESPIPE || ptr->_errno == EINVAL)
+	  if (curoff != -1 || _REENT_ERRNO(ptr) == 0
+	      || _REENT_ERRNO(ptr) == ESPIPE || _REENT_ERRNO(ptr) == EINVAL)
 	    {
 	      /* Seek successful or ignorable error condition.
 		 We can clear read buffer now.  */
@@ -190,9 +190,9 @@ __sflush_r (struct _reent *ptr,
 #endif
 	      fp->_r = 0;
 	      fp->_p = fp->_bf._base;
-	      if ((fp->_flags & __SOFF) && (curoff != -1 || ptr->_errno == 0))
+	      if ((fp->_flags & __SOFF) && (curoff != -1 || _REENT_ERRNO(ptr) == 0))
 		fp->_offset = curoff;
-	      ptr->_errno = tmp_errno;
+	      _REENT_ERRNO(ptr) = tmp_errno;
 	      if (HASUB (fp))
 		FREEUB (ptr, fp);
 	    }
diff --git a/newlib/libc/stdio/fileno.c b/newlib/libc/stdio/fileno.c
index 10fc6e283..4faa6a843 100644
--- a/newlib/libc/stdio/fileno.c
+++ b/newlib/libc/stdio/fileno.c
@@ -73,7 +73,7 @@ fileno (FILE * f)
   else
     {
       result = -1;
-      _REENT->_errno = EBADF;
+      _REENT_ERRNO(_REENT) = EBADF;
     }
   _newlib_flockfile_end (f);
   return result;
diff --git a/newlib/libc/stdio/fileno_u.c b/newlib/libc/stdio/fileno_u.c
index 830a07d2b..f3940e885 100644
--- a/newlib/libc/stdio/fileno_u.c
+++ b/newlib/libc/stdio/fileno_u.c
@@ -39,7 +39,7 @@ fileno_unlocked (FILE * f)
   else
     {
       result = -1;
-      _REENT->_errno = EBADF;
+      _REENT_ERRNO(_REENT) = EBADF;
     }
   return result;
 }
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index ee991ed24..c7a4a941e 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -179,7 +179,7 @@ __sfp (struct _reent *d)
 	break;
     }
   _newlib_sfp_lock_exit ();
-  d->_errno = ENOMEM;
+  _REENT_ERRNO(d) = ENOMEM;
   return NULL;
 
 found:
diff --git a/newlib/libc/stdio/flags.c b/newlib/libc/stdio/flags.c
index 7bbd50181..d686fa046 100644
--- a/newlib/libc/stdio/flags.c
+++ b/newlib/libc/stdio/flags.c
@@ -56,7 +56,7 @@ __sflags (struct _reent *ptr,
       o = O_CREAT | O_APPEND;
       break;
     default:			/* illegal mode */
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return (0);
     }
   while (*++mode)
diff --git a/newlib/libc/stdio/fmemopen.c b/newlib/libc/stdio/fmemopen.c
index 0d043520e..886ba7efe 100644
--- a/newlib/libc/stdio/fmemopen.c
+++ b/newlib/libc/stdio/fmemopen.c
@@ -148,7 +148,7 @@ fmemwriter (struct _reent *ptr,
     memcpy (c->buf + c->pos - n, buf, n - adjust);
   else
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       return EOF;
     }
   return n;
@@ -175,18 +175,18 @@ fmemseeker (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       offset = -1;
     }
   else if (offset > c->max)
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       offset = -1;
     }
 #ifdef __LARGE64_FILES
   else if ((_fpos_t) offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -224,12 +224,12 @@ fmemseeker64 (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       offset = -1;
     }
   else if (offset > c->max)
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       offset = -1;
     }
   else
@@ -277,7 +277,7 @@ _fmemopen_r (struct _reent *ptr,
     return NULL;
   if (!size || !(buf || flags & __SRW))
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/fopencookie.c b/newlib/libc/stdio/fopencookie.c
index 0861528e0..ab406a866 100644
--- a/newlib/libc/stdio/fopencookie.c
+++ b/newlib/libc/stdio/fopencookie.c
@@ -107,7 +107,7 @@ fcreader (struct _reent *ptr,
   fccookie *c = (fccookie *) cookie;
   errno = 0;
   if ((result = c->readfn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 
@@ -129,7 +129,7 @@ fcwriter (struct _reent *ptr,
     }
   errno = 0;
   if ((result = c->writefn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 
@@ -148,11 +148,11 @@ fcseeker (struct _reent *ptr,
 
   errno = 0;
   if (c->seekfn (c->cookie, &offset, whence) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
 #ifdef __LARGE64_FILES
   else if ((_fpos_t)offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -170,7 +170,7 @@ fcseeker64 (struct _reent *ptr,
   fccookie *c = (fccookie *) cookie;
   errno = 0;
   if (c->seekfn (c->cookie, &offset, whence) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return (_fpos64_t) offset;
 }
 #endif /* __LARGE64_FILES */
@@ -185,7 +185,7 @@ fccloser (struct _reent *ptr,
     {
       errno = 0;
       if ((result = c->closefn (c->cookie)) < 0 && errno)
-	ptr->_errno = errno;
+	_REENT_ERRNO(ptr) = errno;
     }
   _free_r (ptr, c);
   return result;
@@ -207,7 +207,7 @@ _fopencookie_r (struct _reent *ptr,
   if (((flags & (__SRD | __SRW)) && !functions.read)
       || ((flags & (__SWR | __SRW)) && !functions.write))
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/fpurge.c b/newlib/libc/stdio/fpurge.c
index 2e4f61ec9..93ac0e2a9 100644
--- a/newlib/libc/stdio/fpurge.c
+++ b/newlib/libc/stdio/fpurge.c
@@ -72,7 +72,7 @@ _fpurge_r (struct _reent *ptr,
   t = fp->_flags;
   if (!t)
     {
-      ptr->_errno = EBADF;
+      _REENT_ERRNO(ptr) = EBADF;
       _newlib_flockfile_exit (fp);
       return EOF;
     }
diff --git a/newlib/libc/stdio/freopen.c b/newlib/libc/stdio/freopen.c
index 29ba5b7ff..f0da9a159 100644
--- a/newlib/libc/stdio/freopen.c
+++ b/newlib/libc/stdio/freopen.c
@@ -137,7 +137,7 @@ _freopen_r (struct _reent *ptr,
   if (file != NULL)
     {
       f = _open_r (ptr, (char *) file, oflags, 0666);
-      e = ptr->_errno;
+      e = _REENT_ERRNO(ptr);
     }
   else
     {
@@ -205,7 +205,7 @@ _freopen_r (struct _reent *ptr,
     {				/* did not get it after all */
       __sfp_lock_acquire ();
       fp->_flags = 0;		/* set it free */
-      ptr->_errno = e;		/* restore in case _close clobbered */
+      _REENT_ERRNO(ptr) = e;	/* restore in case _close clobbered */
       if (!(oflags2 & __SNLK))
 	_funlockfile (fp);
 #ifndef __SINGLE_THREAD__
diff --git a/newlib/libc/stdio/fseeko.c b/newlib/libc/stdio/fseeko.c
index 13df28bfc..6fcc8ef06 100644
--- a/newlib/libc/stdio/fseeko.c
+++ b/newlib/libc/stdio/fseeko.c
@@ -128,7 +128,7 @@ _fseeko_r (struct _reent *ptr,
 
   if ((seekfn = fp->_seek) == NULL)
     {
-      ptr->_errno = ESPIPE;	/* ??? */
+      _REENT_ERRNO(ptr) = ESPIPE;	/* ??? */
       _newlib_flockfile_exit (fp);
       return EOF;
     }
@@ -178,7 +178,7 @@ _fseeko_r (struct _reent *ptr,
       break;
 
     default:
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       _newlib_flockfile_exit (fp);
       return (EOF);
     }
diff --git a/newlib/libc/stdio/ftell.c b/newlib/libc/stdio/ftell.c
index c540c430f..f68b3afcc 100644
--- a/newlib/libc/stdio/ftell.c
+++ b/newlib/libc/stdio/ftell.c
@@ -92,7 +92,7 @@ _ftell_r (struct _reent *ptr,
   if ((long)pos != pos)
     {
       pos = -1;
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
     }
   return (long)pos;
 }
diff --git a/newlib/libc/stdio/ftello.c b/newlib/libc/stdio/ftello.c
index 3df200cea..d62d48a89 100644
--- a/newlib/libc/stdio/ftello.c
+++ b/newlib/libc/stdio/ftello.c
@@ -96,7 +96,7 @@ _ftello_r (struct _reent * ptr,
 
   if (fp->_seek == NULL)
     {
-      ptr->_errno = ESPIPE;
+      _REENT_ERRNO(ptr) = ESPIPE;
       _newlib_flockfile_exit (fp);
       return (_off_t) -1;
     }
diff --git a/newlib/libc/stdio/funopen.c b/newlib/libc/stdio/funopen.c
index a1c0d767d..4ed50196e 100644
--- a/newlib/libc/stdio/funopen.c
+++ b/newlib/libc/stdio/funopen.c
@@ -113,7 +113,7 @@ funreader (struct _reent *ptr,
   funcookie *c = (funcookie *) cookie;
   errno = 0;
   if ((result = c->readfn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 
@@ -127,7 +127,7 @@ funwriter (struct _reent *ptr,
   funcookie *c = (funcookie *) cookie;
   errno = 0;
   if ((result = c->writefn (c->cookie, buf, n)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 
@@ -142,15 +142,15 @@ funseeker (struct _reent *ptr,
   fpos_t result;
   errno = 0;
   if ((result = c->seekfn (c->cookie, (fpos_t) off, whence)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
 #else /* __LARGE64_FILES */
   _fpos64_t result;
   errno = 0;
   if ((result = c->seekfn (c->cookie, (_fpos64_t) off, whence)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   else if ((_fpos_t)result != result)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       result = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -168,7 +168,7 @@ funseeker64 (struct _reent *ptr,
   funcookie *c = (funcookie *) cookie;
   errno = 0;
   if ((result = c->seekfn (c->cookie, off, whence)) < 0 && errno)
-    ptr->_errno = errno;
+    _REENT_ERRNO(ptr) = errno;
   return result;
 }
 #endif /* __LARGE64_FILES */
@@ -183,7 +183,7 @@ funcloser (struct _reent *ptr,
     {
       errno = 0;
       if ((result = c->closefn (c->cookie)) < 0 && errno)
-	ptr->_errno = errno;
+	_REENT_ERRNO(ptr) = errno;
     }
   _free_r (ptr, c);
   return result;
@@ -202,7 +202,7 @@ _funopen_r (struct _reent *ptr,
 
   if (!readfn && !writefn)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/fvwrite.c b/newlib/libc/stdio/fvwrite.c
index 587ffacd7..34361cb8d 100644
--- a/newlib/libc/stdio/fvwrite.c
+++ b/newlib/libc/stdio/fvwrite.c
@@ -145,7 +145,7 @@ __sfvwrite_r (struct _reent *ptr,
 		      str = (unsigned char *)_malloc_r (ptr, newsize);
 		      if (!str)
 			{
-			  ptr->_errno = ENOMEM;
+			  _REENT_ERRNO(ptr) = ENOMEM;
 			  goto err;
 			}
 		      memcpy (str, fp->_bf._base, curpos);
@@ -162,7 +162,7 @@ __sfvwrite_r (struct _reent *ptr,
 			  _free_r (ptr, fp->_bf._base);
 			  fp->_flags &=  ~__SMBF;
 			  /* Ensure correct errno, even if free changed it.  */
-			  ptr->_errno = ENOMEM;
+			  _REENT_ERRNO(ptr) = ENOMEM;
 			  goto err;
 			}
 		    }
diff --git a/newlib/libc/stdio/mktemp.c b/newlib/libc/stdio/mktemp.c
index 3514818a6..265968198 100644
--- a/newlib/libc/stdio/mktemp.c
+++ b/newlib/libc/stdio/mktemp.c
@@ -159,7 +159,7 @@ _gettemp (struct _reent *ptr,
     continue;
   if (trv - path < suffixlen)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return 0;
     }
   trv -= suffixlen;
@@ -171,7 +171,7 @@ _gettemp (struct _reent *ptr,
     }
   if (end - trv < 6)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return 0;
     }
 
@@ -195,7 +195,7 @@ _gettemp (struct _reent *ptr,
 	    return (0);
 	  if (!(sbuf.st_mode & S_IFDIR))
 	    {
-	      ptr->_errno = ENOTDIR;
+	      _REENT_ERRNO(ptr) = ENOTDIR;
 	      return (0);
 	    }
 	  *trv = '/';
@@ -211,10 +211,10 @@ _gettemp (struct _reent *ptr,
 #ifdef HAVE_MKDIR
 	  if (_mkdir_r (ptr, path, 0700) == 0)
 	    return 1;
-	  if (ptr->_errno != EEXIST)
+	  if (_REENT_ERRNO(ptr) != EEXIST)
 	    return 0;
 #else /* !HAVE_MKDIR */
-	  ptr->_errno = ENOSYS;
+	  _REENT_ERRNO(ptr) = ENOSYS;
 	  return 0;
 #endif /* !HAVE_MKDIR */
 	}
@@ -225,7 +225,7 @@ _gettemp (struct _reent *ptr,
 	  if ((*doopen = _open_r (ptr, path, O_CREAT | O_EXCL | O_RDWR | flags,
 				  0600)) >= 0)
 	    return 1;
-	  if (ptr->_errno != EEXIST)
+	  if (_REENT_ERRNO(ptr) != EEXIST)
 	    return 0;
 	}
 #ifdef __USE_INTERNAL_STAT64
@@ -233,7 +233,7 @@ _gettemp (struct _reent *ptr,
 #else
       else if (_stat_r (ptr, path, &sbuf))
 #endif
-	return (ptr->_errno == ENOENT ? 1 : 0);
+	return (_REENT_ERRNO(ptr) == ENOENT ? 1 : 0);
 
       /* tricky little algorithm for backward compatibility */
       for (trv = start;;)
diff --git a/newlib/libc/stdio/nano-vfprintf.c b/newlib/libc/stdio/nano-vfprintf.c
index 838804eb9..0d42a940f 100644
--- a/newlib/libc/stdio/nano-vfprintf.c
+++ b/newlib/libc/stdio/nano-vfprintf.c
@@ -201,7 +201,7 @@ __ssputs_r (struct _reent *ptr,
 	  str = (unsigned char *)_malloc_r (ptr, newsize);
 	  if (!str)
 	    {
-	      ptr->_errno = ENOMEM;
+	      _REENT_ERRNO(ptr) = ENOMEM;
 	      goto err;
 	    }
 	  memcpy (str, fp->_bf._base, curpos);
@@ -215,7 +215,7 @@ __ssputs_r (struct _reent *ptr,
 	      /* Free unneeded buffer.  */
 	      _free_r (ptr, fp->_bf._base);
 	      /* Ensure correct errno, even if free changed it.  */
-	      ptr->_errno = ENOMEM;
+	      _REENT_ERRNO(ptr) = ENOMEM;
 	      goto err;
 	    }
 	}
@@ -291,7 +291,7 @@ __ssprint_r (struct _reent *ptr,
 	      str = (unsigned char *)_malloc_r (ptr, newsize);
 	      if (!str)
 		{
-		  ptr->_errno = ENOMEM;
+		  _REENT_ERRNO(ptr) = ENOMEM;
 		  goto err;
 		}
 	      memcpy (str, fp->_bf._base, curpos);
@@ -306,7 +306,7 @@ __ssprint_r (struct _reent *ptr,
 		  /* Free unneeded buffer.  */
 		  _free_r (ptr, fp->_bf._base);
 		  /* Ensure correct errno, even if free changed it.  */
-		  ptr->_errno = ENOMEM;
+		  _REENT_ERRNO(ptr) = ENOMEM;
 		  goto err;
 		}
 	    }
@@ -501,7 +501,7 @@ _VFPRINTF_R (struct _reent *data,
       fp->_bf._base = fp->_p = _malloc_r (data, 64);
       if (!fp->_p)
 	{
-	  data->_errno = ENOMEM;
+	  _REENT_ERRNO(data) = ENOMEM;
 	  return EOF;
 	}
       fp->_bf._size = 64;
diff --git a/newlib/libc/stdio/open_memstream.c b/newlib/libc/stdio/open_memstream.c
index ed71b1cdc..e3a52ed33 100644
--- a/newlib/libc/stdio/open_memstream.c
+++ b/newlib/libc/stdio/open_memstream.c
@@ -105,7 +105,7 @@ memwriter (struct _reent *ptr,
      big that user cannot do ftello.  */
   if (sizeof (OFF_T) == sizeof (size_t) && (ssize_t) (c->pos + n) < 0)
     {
-      ptr->_errno = EFBIG;
+      _REENT_ERRNO(ptr) = EFBIG;
       return EOF;
     }
   /* Grow the buffer, if necessary.  Choose a geometric growth factor
@@ -160,18 +160,18 @@ memseeker (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       offset = -1;
     }
   else if ((size_t) offset != offset)
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       offset = -1;
     }
 #ifdef __LARGE64_FILES
   else if ((_fpos_t) offset != offset)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       offset = -1;
     }
 #endif /* __LARGE64_FILES */
@@ -227,12 +227,12 @@ memseeker64 (struct _reent *ptr,
     offset += c->eof;
   if (offset < 0)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       offset = -1;
     }
   else if ((size_t) offset != offset)
     {
-      ptr->_errno = ENOSPC;
+      _REENT_ERRNO(ptr) = ENOSPC;
       offset = -1;
     }
   else
@@ -301,7 +301,7 @@ internal_open_memstream_r (struct _reent *ptr,
 
   if (!buf || !size)
     {
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       return NULL;
     }
   if ((fp = __sfp (ptr)) == NULL)
diff --git a/newlib/libc/stdio/perror.c b/newlib/libc/stdio/perror.c
index eda7e993b..2033a6f2f 100644
--- a/newlib/libc/stdio/perror.c
+++ b/newlib/libc/stdio/perror.c
@@ -90,7 +90,7 @@ _perror_r (struct _reent *ptr,
       WRITE_STR (": ");
     }
 
-  if ((error = _strerror_r (ptr, ptr->_errno, 1, &dummy)) != NULL)
+  if ((error = _strerror_r (ptr, _REENT_ERRNO(ptr), 1, &dummy)) != NULL)
     WRITE_STR (error);
 
 #ifdef __SCLE
diff --git a/newlib/libc/stdio/refill.c b/newlib/libc/stdio/refill.c
index 91e79e832..7bd38806c 100644
--- a/newlib/libc/stdio/refill.c
+++ b/newlib/libc/stdio/refill.c
@@ -56,7 +56,7 @@ __srefill_r (struct _reent * ptr,
     {
       if ((fp->_flags & __SRW) == 0)
 	{
-	  ptr->_errno = EBADF;
+	  _REENT_ERRNO(ptr) = EBADF;
 	  fp->_flags |= __SERR;
 	  return EOF;
 	}
diff --git a/newlib/libc/stdio/sniprintf.c b/newlib/libc/stdio/sniprintf.c
index 375a3971f..b05ffcb12 100644
--- a/newlib/libc/stdio/sniprintf.c
+++ b/newlib/libc/stdio/sniprintf.c
@@ -38,7 +38,7 @@ _sniprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -49,7 +49,7 @@ _sniprintf_r (struct _reent *ptr,
   ret = _svfiprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
@@ -69,7 +69,7 @@ sniprintf (char *str,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -80,7 +80,7 @@ sniprintf (char *str,
   ret = _svfiprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
diff --git a/newlib/libc/stdio/snprintf.c b/newlib/libc/stdio/snprintf.c
index 50d7ddd11..4a3084bea 100644
--- a/newlib/libc/stdio/snprintf.c
+++ b/newlib/libc/stdio/snprintf.c
@@ -37,7 +37,7 @@ _snprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -48,7 +48,7 @@ _snprintf_r (struct _reent *ptr,
   ret = _svfprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
@@ -74,7 +74,7 @@ snprintf (char *__restrict str,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -85,7 +85,7 @@ snprintf (char *__restrict str,
   ret = _svfprintf_r (ptr, &f, fmt, ap);
   va_end (ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return (ret);
diff --git a/newlib/libc/stdio/swprintf.c b/newlib/libc/stdio/swprintf.c
index ec7e089ca..634b22beb 100644
--- a/newlib/libc/stdio/swprintf.c
+++ b/newlib/libc/stdio/swprintf.c
@@ -564,7 +564,7 @@ _swprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX / sizeof (wchar_t))
     {
-      ptr->_errno = EOVERFLOW;	/* POSIX extension */
+      _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -584,7 +584,7 @@ _swprintf_r (struct _reent *ptr,
     /* _svfwprintf_r() returns how many wide characters it would have printed
      * if there were enough space.  Return an error if too big to fit in str,
      * unlike snprintf, which returns the size needed.  */
-    ptr->_errno = EOVERFLOW;	/* POSIX extension */
+    _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return (ret);
@@ -604,7 +604,7 @@ swprintf (wchar_t *__restrict str,
 
   if (size > INT_MAX / sizeof (wchar_t))
     {
-      ptr->_errno = EOVERFLOW;	/* POSIX extension */
+      _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -624,7 +624,7 @@ swprintf (wchar_t *__restrict str,
     /* _svfwprintf_r() returns how many wide characters it would have printed
      * if there were enough space.  Return an error if too big to fit in str,
      * unlike snprintf, which returns the size needed.  */
-    ptr->_errno = EOVERFLOW;	/* POSIX extension */
+    _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return (ret);
diff --git a/newlib/libc/stdio/tmpfile.c b/newlib/libc/stdio/tmpfile.c
index f209a3edf..5fe6e3bf1 100644
--- a/newlib/libc/stdio/tmpfile.c
+++ b/newlib/libc/stdio/tmpfile.c
@@ -65,15 +65,15 @@ _tmpfile_r (struct _reent *ptr)
       fd = _open_r (ptr, f, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
 		    S_IRUSR | S_IWUSR);
     }
-  while (fd < 0 && ptr->_errno == EEXIST);
+  while (fd < 0 && _REENT_ERRNO(ptr) == EEXIST);
   if (fd < 0)
     return NULL;
   fp = _fdopen_r (ptr, fd, "wb+");
-  e = ptr->_errno;
+  e = _REENT_ERRNO(ptr);
   if (!fp)
     _close_r (ptr, fd);
   (void) _remove_r (ptr, f);
-  ptr->_errno = e;
+  _REENT_ERRNO(ptr) = e;
   return fp;
 }
 
diff --git a/newlib/libc/stdio/tmpnam.c b/newlib/libc/stdio/tmpnam.c
index 3bd5b6a58..7379a7640 100644
--- a/newlib/libc/stdio/tmpnam.c
+++ b/newlib/libc/stdio/tmpnam.c
@@ -104,7 +104,7 @@ worker (struct _reent *ptr,
       t = _open_r (ptr, result, O_RDONLY, 0);
       if (t == -1)
 	{
-	  if (ptr->_errno == ENOSYS)
+	  if (_REENT_ERRNO(ptr) == ENOSYS)
 	    {
 	      result[0] = '\0';
 	      return 0;
diff --git a/newlib/libc/stdio/vasniprintf.c b/newlib/libc/stdio/vasniprintf.c
index edfbd2242..b24cd96fb 100644
--- a/newlib/libc/stdio/vasniprintf.c
+++ b/newlib/libc/stdio/vasniprintf.c
@@ -42,7 +42,7 @@ _vasniprintf_r (struct _reent *ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/vasnprintf.c b/newlib/libc/stdio/vasnprintf.c
index 1fbf5a171..b3787ec7a 100644
--- a/newlib/libc/stdio/vasnprintf.c
+++ b/newlib/libc/stdio/vasnprintf.c
@@ -42,7 +42,7 @@ _vasnprintf_r (struct _reent *ptr,
      for _size.  */
   if (len > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return NULL;
     }
   f._bf._size = f._w = len;
diff --git a/newlib/libc/stdio/vfprintf.c b/newlib/libc/stdio/vfprintf.c
index 32ebb1468..6a198e2c6 100644
--- a/newlib/libc/stdio/vfprintf.c
+++ b/newlib/libc/stdio/vfprintf.c
@@ -222,7 +222,7 @@ __ssputs_r (struct _reent *ptr,
 			str = (unsigned char *)_malloc_r (ptr, newsize);
 			if (!str)
 			{
-				ptr->_errno = ENOMEM;
+				_REENT_ERRNO(ptr) = ENOMEM;
 				goto err;
 			}
 			memcpy (str, fp->_bf._base, curpos);
@@ -237,7 +237,7 @@ __ssputs_r (struct _reent *ptr,
 				_free_r (ptr, fp->_bf._base);
 				/* Ensure correct errno, even if free
 				 * changed it.  */
-				ptr->_errno = ENOMEM;
+				_REENT_ERRNO(ptr) = ENOMEM;
 				goto err;
 			}
 		}
@@ -306,7 +306,7 @@ __ssprint_r (struct _reent *ptr,
 				str = (unsigned char *)_malloc_r (ptr, newsize);
 				if (!str)
 				{
-					ptr->_errno = ENOMEM;
+					_REENT_ERRNO(ptr) = ENOMEM;
 					goto err;
 				}
 				memcpy (str, fp->_bf._base, curpos);
@@ -321,7 +321,7 @@ __ssprint_r (struct _reent *ptr,
 					_free_r (ptr, fp->_bf._base);
 					/* Ensure correct errno, even if free
 					 * changed it.  */
-					ptr->_errno = ENOMEM;
+					_REENT_ERRNO(ptr) = ENOMEM;
 					goto err;
 				}
 			}
@@ -868,7 +868,7 @@ _VFPRINTF_R (struct _reent *data,
 		fp->_bf._base = fp->_p = _malloc_r (data, 64);
 		if (!fp->_p)
 		{
-			data->_errno = ENOMEM;
+			_REENT_ERRNO(data) = ENOMEM;
 			return EOF;
 		}
 		fp->_bf._size = 64;
@@ -1374,7 +1374,7 @@ reswitch:	switch (ch) {
 		case 'm':  /* extension */
 			{
 				int dummy;
-				cp = _strerror_r (data, data->_errno, 1, &dummy);
+				cp = _strerror_r (data, _REENT_ERRNO(data), 1, &dummy);
 			}
 			flags &= ~LONGINT;
 			goto string;
diff --git a/newlib/libc/stdio/vfscanf.c b/newlib/libc/stdio/vfscanf.c
index 037692531..cfeea9876 100644
--- a/newlib/libc/stdio/vfscanf.c
+++ b/newlib/libc/stdio/vfscanf.c
@@ -774,7 +774,7 @@ __SVFSCANF_R (struct _reent *rptr,
 	      width = 0;
 	      goto again;
 	    }
-	  rptr->_errno = EINVAL;
+	  _REENT_ERRNO(rptr) = EINVAL;
 	  goto input_failure;
 #endif /* !_NO_POS_ARGS */
 
diff --git a/newlib/libc/stdio/vfwprintf.c b/newlib/libc/stdio/vfwprintf.c
index 7384b37d3..7807a1229 100644
--- a/newlib/libc/stdio/vfwprintf.c
+++ b/newlib/libc/stdio/vfwprintf.c
@@ -611,7 +611,7 @@ _VFWPRINTF_R (struct _reent *data,
 		fp->_bf._base = fp->_p = _malloc_r (data, 64);
 		if (!fp->_p)
 		{
-			data->_errno = ENOMEM;
+			_REENT_ERRNO(data) = ENOMEM;
 			return EOF;
 		}
 		fp->_bf._size = 64;
@@ -1119,7 +1119,7 @@ reswitch:	switch (ch) {
 		case L'm':  /* GNU extension */
 			{
 				int dummy;
-				cp = (wchar_t *) _strerror_r (data, data->_errno, 1, &dummy);
+				cp = (wchar_t *) _strerror_r (data, _REENT_ERRNO(data), 1, &dummy);
 			}
 			flags &= ~LONGINT;
 			goto string;
diff --git a/newlib/libc/stdio/vfwscanf.c b/newlib/libc/stdio/vfwscanf.c
index e9e00dfec..df966f929 100644
--- a/newlib/libc/stdio/vfwscanf.c
+++ b/newlib/libc/stdio/vfwscanf.c
@@ -682,7 +682,7 @@ __SVFWSCANF_R (struct _reent *rptr,
 	      width = 0;
 	      goto again;
 	    }
-	  rptr->_errno = EINVAL;
+	  _REENT_ERRNO(rptr) = EINVAL;
 	  goto input_failure;
 #endif /* !_NO_POS_ARGS */
 
diff --git a/newlib/libc/stdio/vsniprintf.c b/newlib/libc/stdio/vsniprintf.c
index a8b272e2f..e7625ffd5 100644
--- a/newlib/libc/stdio/vsniprintf.c
+++ b/newlib/libc/stdio/vsniprintf.c
@@ -54,7 +54,7 @@ _vsniprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -63,7 +63,7 @@ _vsniprintf_r (struct _reent *ptr,
   f._file = -1;  /* No file. */
   ret = _svfiprintf_r (ptr, &f, fmt, ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return ret;
diff --git a/newlib/libc/stdio/vsnprintf.c b/newlib/libc/stdio/vsnprintf.c
index 5c617a8a3..ecf82eaef 100644
--- a/newlib/libc/stdio/vsnprintf.c
+++ b/newlib/libc/stdio/vsnprintf.c
@@ -60,7 +60,7 @@ _vsnprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX)
     {
-      ptr->_errno = EOVERFLOW;
+      _REENT_ERRNO(ptr) = EOVERFLOW;
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -69,7 +69,7 @@ _vsnprintf_r (struct _reent *ptr,
   f._file = -1;  /* No file. */
   ret = _svfprintf_r (ptr, &f, fmt, ap);
   if (ret < EOF)
-    ptr->_errno = EOVERFLOW;
+    _REENT_ERRNO(ptr) = EOVERFLOW;
   if (size > 0)
     *f._p = 0;
   return ret;
diff --git a/newlib/libc/stdio/vswprintf.c b/newlib/libc/stdio/vswprintf.c
index 89795ed8d..08a8545ba 100644
--- a/newlib/libc/stdio/vswprintf.c
+++ b/newlib/libc/stdio/vswprintf.c
@@ -42,7 +42,7 @@ _vswprintf_r (struct _reent *ptr,
 
   if (size > INT_MAX / sizeof (wchar_t))
     {
-      ptr->_errno = EOVERFLOW;	/* POSIX extension */
+      _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
       return EOF;
     }
   f._flags = __SWR | __SSTR;
@@ -60,7 +60,7 @@ _vswprintf_r (struct _reent *ptr,
     /* _svfwprintf_r() returns how many wide characters it would have printed
      * if there were enough space.  Return an error if too big to fit in str,
      * unlike snprintf, which returns the size needed.  */
-    ptr->_errno = EOVERFLOW;	/* POSIX extension */
+    _REENT_ERRNO(ptr) = EOVERFLOW;	/* POSIX extension */
     ret = -1;
   }
   return ret;
diff --git a/newlib/libc/stdio/wsetup.c b/newlib/libc/stdio/wsetup.c
index 6a820f1c0..0fd9690aa 100644
--- a/newlib/libc/stdio/wsetup.c
+++ b/newlib/libc/stdio/wsetup.c
@@ -45,7 +45,7 @@ __swsetup_r (struct _reent *ptr,
     {
       if ((fp->_flags & __SRW) == 0)
         {
-	  ptr->_errno = EBADF;
+	  _REENT_ERRNO(ptr) = EBADF;
 	  fp->_flags |= __SERR;
 	  return EOF;
         }
diff --git a/newlib/libc/stdio64/fdopen64.c b/newlib/libc/stdio64/fdopen64.c
index 9d9645b36..d93b3d4d8 100644
--- a/newlib/libc/stdio64/fdopen64.c
+++ b/newlib/libc/stdio64/fdopen64.c
@@ -55,7 +55,7 @@ _fdopen64_r (struct _reent *ptr,
   fdmode = fdflags & O_ACCMODE;
   if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
     {
-      ptr->_errno = EBADF;
+      _REENT_ERRNO(ptr) = EBADF;
       return 0;
     }
 #endif
diff --git a/newlib/libc/stdio64/freopen64.c b/newlib/libc/stdio64/freopen64.c
index e6ba64f7d..4d1e9b76c 100644
--- a/newlib/libc/stdio64/freopen64.c
+++ b/newlib/libc/stdio64/freopen64.c
@@ -138,7 +138,7 @@ _freopen64_r (struct _reent *ptr,
   if (file != NULL)
     {
       f = _open64_r (ptr, (char *) file, oflags, 0666);
-      e = ptr->_errno;
+      e = _REENT_ERRNO(ptr);
     }
   else
     {
@@ -206,7 +206,7 @@ _freopen64_r (struct _reent *ptr,
     {				/* did not get it after all */
       __sfp_lock_acquire ();
       fp->_flags = 0;		/* set it free */
-      ptr->_errno = e;		/* restore in case _close clobbered */
+      _REENT_ERRNO(ptr) = e;	/* restore in case _close clobbered */
       if (!(oflags2 & __SNLK))
 	_funlockfile (fp);
 #ifndef __SINGLE_THREAD__
diff --git a/newlib/libc/stdio64/fseeko64.c b/newlib/libc/stdio64/fseeko64.c
index 3087bef9e..c5b30aed0 100644
--- a/newlib/libc/stdio64/fseeko64.c
+++ b/newlib/libc/stdio64/fseeko64.c
@@ -102,7 +102,7 @@ _fseeko64_r (struct _reent *ptr,
     {
       if ((_off_t) offset != offset)
 	{
-	  ptr->_errno = EOVERFLOW;
+	  _REENT_ERRNO(ptr) = EOVERFLOW;
 	  return EOF;
 	}
       return (_off64_t) _fseeko_r (ptr, fp, offset, whence);
@@ -129,7 +129,7 @@ _fseeko64_r (struct _reent *ptr,
 
   if ((seekfn = fp->_seek64) == NULL)
     {
-      ptr->_errno = ESPIPE;	/* ??? */
+      _REENT_ERRNO(ptr) = ESPIPE;	/* ??? */
       _newlib_flockfile_exit(fp);
       return EOF;
     }
@@ -179,7 +179,7 @@ _fseeko64_r (struct _reent *ptr,
       break;
 
     default:
-      ptr->_errno = EINVAL;
+      _REENT_ERRNO(ptr) = EINVAL;
       _newlib_flockfile_exit(fp);
       return (EOF);
     }
diff --git a/newlib/libc/stdio64/ftello64.c b/newlib/libc/stdio64/ftello64.c
index 6fb76c3ef..cd00def46 100644
--- a/newlib/libc/stdio64/ftello64.c
+++ b/newlib/libc/stdio64/ftello64.c
@@ -93,7 +93,7 @@ _ftello64_r (struct _reent *ptr,
 
   if (fp->_seek64 == NULL)
     {
-      ptr->_errno = ESPIPE;
+      _REENT_ERRNO(ptr) = ESPIPE;
       _newlib_flockfile_exit(fp);
       return (_off64_t) -1;
     }
diff --git a/newlib/libc/stdio64/tmpfile64.c b/newlib/libc/stdio64/tmpfile64.c
index 18a38d65c..35b035c91 100644
--- a/newlib/libc/stdio64/tmpfile64.c
+++ b/newlib/libc/stdio64/tmpfile64.c
@@ -68,15 +68,15 @@ _tmpfile64_r (struct _reent *ptr)
       fd = _open64_r (ptr, f, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
 		      S_IRUSR | S_IWUSR);
   }
-  while (fd < 0 && ptr->_errno == EEXIST);
+  while (fd < 0 && _REENT_ERRNO(ptr) == EEXIST);
   if (fd < 0)
     return NULL;
   fp = _fdopen64_r (ptr, fd, "wb+");
-  e = ptr->_errno;
+  e = _REENT_ERRNO(ptr);
   if (!fp)
     _close_r (ptr, fd);
   (void) _remove_r (ptr, f);
-  ptr->_errno = e;
+  _REENT_ERRNO(ptr) = e;
   return fp;
 }
 
diff --git a/newlib/libc/stdlib/__adjust.c b/newlib/libc/stdlib/__adjust.c
index 4c478f30b..ab6f125b9 100644
--- a/newlib/libc/stdlib/__adjust.c
+++ b/newlib/libc/stdlib/__adjust.c
@@ -21,12 +21,12 @@ __adjust (struct _reent *ptr,
 
   if (dexp > MAXE)
     {
-      ptr->_errno = ERANGE;
+      _REENT_ERRNO(ptr) = ERANGE;
       return (sign) ? -HUGE_VAL : HUGE_VAL;
     }
   else if (dexp < MINE)
     {
-      ptr->_errno = ERANGE;
+      _REENT_ERRNO(ptr) = ERANGE;
       return 0.0;
     }
 
diff --git a/newlib/libc/stdlib/mbrtowc.c b/newlib/libc/stdlib/mbrtowc.c
index 65284a0eb..521b7a5f4 100644
--- a/newlib/libc/stdlib/mbrtowc.c
+++ b/newlib/libc/stdlib/mbrtowc.c
@@ -32,7 +32,7 @@ _mbrtowc_r (struct _reent *ptr,
   if (retval == -1)
     {
       ps->__count = 0;
-      ptr->_errno = EILSEQ;
+      _REENT_ERRNO(ptr) = EILSEQ;
       return (size_t)(-1);
     }
   else
@@ -68,7 +68,7 @@ mbrtowc (wchar_t *__restrict pwc,
   if (retval == -1)
     {
       ps->__count = 0;
-      reent->_errno = EILSEQ;
+      _REENT_ERRNO(reent) = EILSEQ;
       return (size_t)(-1);
     }
   else
diff --git a/newlib/libc/stdlib/mbsnrtowcs.c b/newlib/libc/stdlib/mbsnrtowcs.c
index d3ce25084..8f94b1da5 100644
--- a/newlib/libc/stdlib/mbsnrtowcs.c
+++ b/newlib/libc/stdlib/mbsnrtowcs.c
@@ -126,7 +126,7 @@ _mbsnrtowcs_r (struct _reent *r,
       else
 	{
 	  ps->__count = 0;
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return (size_t)-1;
 	}
     }
diff --git a/newlib/libc/stdlib/mbtowc_r.c b/newlib/libc/stdlib/mbtowc_r.c
index 920a7ea3c..ca876f9a0 100644
--- a/newlib/libc/stdlib/mbtowc_r.c
+++ b/newlib/libc/stdlib/mbtowc_r.c
@@ -39,7 +39,7 @@ __ascii_mbtowc (struct _reent *r,
 #ifdef __CYGWIN__
   if ((wchar_t)*t >= 0x80)
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
 #endif
@@ -117,7 +117,7 @@ ___iso_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
 	  *pwc = __iso_8859_conv[iso_idx][*t - 0xa0];
 	  if (*pwc == 0) /* Invalid character */
 	    {
-	      r->_errno = EILSEQ;
+	      _REENT_ERRNO(r) = EILSEQ;
 	      return -1;
 	    }
 	  return 1;
@@ -290,7 +290,7 @@ ___cp_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
 	  *pwc = __cp_conv[cp_idx][*t - 0x80];
 	  if (*pwc == 0) /* Invalid character */
 	    {
-	      r->_errno = EILSEQ;
+	      _REENT_ERRNO(r) = EILSEQ;
 	      return -1;
 	    }
 	  return 1;
@@ -578,13 +578,13 @@ __utf8_mbtowc (struct _reent *r,
       ch = t[i++];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       if (state->__value.__wchb[0] < 0xc2)
 	{
 	  /* overlong UTF-8 sequence */
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__count = 0;
@@ -607,12 +607,12 @@ __utf8_mbtowc (struct _reent *r,
       if (state->__value.__wchb[0] == 0xe0 && ch < 0xa0)
 	{
 	  /* overlong UTF-8 sequence */
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__value.__wchb[1] = ch;
@@ -625,7 +625,7 @@ __utf8_mbtowc (struct _reent *r,
       ch = t[i++];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__count = 0;
@@ -651,12 +651,12 @@ __utf8_mbtowc (struct _reent *r,
 	  || (state->__value.__wchb[0] == 0xf4 && ch >= 0x90))
 	{
 	  /* overlong UTF-8 sequence or result is > 0x10ffff */
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__value.__wchb[1] = ch;
@@ -669,7 +669,7 @@ __utf8_mbtowc (struct _reent *r,
       ch = (state->__count == 2) ? t[i++] : state->__value.__wchb[2];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       state->__value.__wchb[2] = ch;
@@ -702,7 +702,7 @@ __utf8_mbtowc (struct _reent *r,
       ch = t[i++];
       if (ch < 0x80 || ch > 0xbf)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
       tmp = (wint_t)((state->__value.__wchb[0] & 0x07) << 18)
@@ -719,7 +719,7 @@ __utf8_mbtowc (struct _reent *r,
       return i;
     }
 
-  r->_errno = EILSEQ;
+  _REENT_ERRNO(r) = EILSEQ;
   return -1;
 }
 
@@ -769,7 +769,7 @@ __sjis_mbtowc (struct _reent *r,
 	}
       else  
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -836,7 +836,7 @@ __eucjp_mbtowc (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -851,7 +851,7 @@ __eucjp_mbtowc (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -955,7 +955,7 @@ __jis_mbtowc (struct _reent *r,
 	  break;
 	case ERROR:
 	default:
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
 
diff --git a/newlib/libc/stdlib/strtod.c b/newlib/libc/stdlib/strtod.c
index cd0222481..9156ed7d6 100644
--- a/newlib/libc/stdlib/strtod.c
+++ b/newlib/libc/stdlib/strtod.c
@@ -595,7 +595,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se,
 			if (e1 > DBL_MAX_10_EXP) {
  ovfl:
 #ifndef NO_ERRNO
-				ptr->_errno = ERANGE;
+				_REENT_ERRNO(ptr) = ERANGE;
 #endif
 				/* Can't trust HUGE_VAL */
 #ifdef IEEE_Arith
@@ -702,7 +702,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se,
  undfl:
 					dval(rv) = 0.;
 #ifndef NO_ERRNO
-					ptr->_errno = ERANGE;
+					_REENT_ERRNO(ptr) = ERANGE;
 #endif
 					if (bd0)
 						goto retfree;
@@ -1249,7 +1249,7 @@ _strtod_l (struct _reent *ptr, const char *__restrict s00, char **__restrict se,
 #ifndef NO_ERRNO
 		/* try to avoid the bug of testing an 8087 register value */
 		if ((dword0(rv) & Exp_mask) == 0)
-			ptr->_errno = ERANGE;
+			_REENT_ERRNO(ptr) = ERANGE;
 #endif
 		}
 #endif /* Avoid_Underflow */
@@ -1303,7 +1303,7 @@ strtof_l (const char *__restrict s00, char **__restrict se, locale_t loc)
   float retval = (float) val;
 #ifndef NO_ERRNO
   if (isinf (retval) && !isinf (val))
-    _REENT->_errno = ERANGE;
+    _REENT_ERRNO(_REENT) = ERANGE;
 #endif
   return retval;
 }
@@ -1340,7 +1340,7 @@ strtof (const char *__restrict s00,
   float retval = (float) val;
 #ifndef NO_ERRNO
   if ((isinf (retval) && !isinf (val)) || (isdenormf(retval) && !isdenorm(val)))
-    _REENT->_errno = ERANGE;
+    _REENT_ERRNO(_REENT) = ERANGE;
 #endif
   return retval;
 }
diff --git a/newlib/libc/stdlib/strtoimax.c b/newlib/libc/stdlib/strtoimax.c
index c3f27df2e..a64b7da66 100644
--- a/newlib/libc/stdlib/strtoimax.c
+++ b/newlib/libc/stdlib/strtoimax.c
@@ -136,10 +136,10 @@ _strtoimax_l(struct _reent *rptr, const char * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? INTMAX_MIN : INTMAX_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/strtol.c b/newlib/libc/stdlib/strtol.c
index 6383c27e8..09d4333ed 100644
--- a/newlib/libc/stdlib/strtol.c
+++ b/newlib/libc/stdlib/strtol.c
@@ -204,7 +204,7 @@ _strtol_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_MIN : LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoll.c b/newlib/libc/stdlib/strtoll.c
index 9aa2c747b..0f2fa5315 100644
--- a/newlib/libc/stdlib/strtoll.c
+++ b/newlib/libc/stdlib/strtoll.c
@@ -201,7 +201,7 @@ _strtoll_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_LONG_MIN : LONG_LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoul.c b/newlib/libc/stdlib/strtoul.c
index 4191e43ec..d31bde5d2 100644
--- a/newlib/libc/stdlib/strtoul.c
+++ b/newlib/libc/stdlib/strtoul.c
@@ -178,7 +178,7 @@ _strtoul_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = ULONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoull.c b/newlib/libc/stdlib/strtoull.c
index 10018ca0d..943de6b83 100644
--- a/newlib/libc/stdlib/strtoull.c
+++ b/newlib/libc/stdlib/strtoull.c
@@ -176,7 +176,7 @@ _strtoull_l (struct _reent *rptr, const char *__restrict nptr,
 	}
 	if (any < 0) {
 		acc = ULONG_LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/strtoumax.c b/newlib/libc/stdlib/strtoumax.c
index cf1a427fe..adcf2b338 100644
--- a/newlib/libc/stdlib/strtoumax.c
+++ b/newlib/libc/stdlib/strtoumax.c
@@ -115,10 +115,10 @@ _strtoumax_l(struct _reent *rptr, const char * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = UINTMAX_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/wcrtomb.c b/newlib/libc/stdlib/wcrtomb.c
index 97436cb74..6d670e23a 100644
--- a/newlib/libc/stdlib/wcrtomb.c
+++ b/newlib/libc/stdlib/wcrtomb.c
@@ -31,7 +31,7 @@ _wcrtomb_r (struct _reent *ptr,
   if (retval == -1)
     {
       ps->__count = 0;
-      ptr->_errno = EILSEQ;
+      _REENT_ERRNO(ptr) = EILSEQ;
       return (size_t)(-1);
     }
   else
@@ -67,7 +67,7 @@ wcrtomb (char *__restrict s,
   if (retval == -1)
     {
       ps->__count = 0;
-      reent->_errno = EILSEQ;
+      _REENT_ERRNO(reent) = EILSEQ;
       return (size_t)(-1);
     }
   else
diff --git a/newlib/libc/stdlib/wcsnrtombs.c b/newlib/libc/stdlib/wcsnrtombs.c
index 43dd2f3e6..dfd974f24 100644
--- a/newlib/libc/stdlib/wcsnrtombs.c
+++ b/newlib/libc/stdlib/wcsnrtombs.c
@@ -104,7 +104,7 @@ _wcsnrtombs_l (struct _reent *r, char *dst, const wchar_t **src, size_t nwc,
       int bytes = loc->wctomb (r, buff, *pwcs, ps);
       if (bytes == -1)
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  ps->__count = 0;
 	  return (size_t)-1;
 	}
diff --git a/newlib/libc/stdlib/wcstod.c b/newlib/libc/stdlib/wcstod.c
index 375ffe288..7b0983470 100644
--- a/newlib/libc/stdlib/wcstod.c
+++ b/newlib/libc/stdlib/wcstod.c
@@ -257,7 +257,7 @@ wcstof_l (const wchar_t *__restrict nptr, wchar_t **__restrict endptr,
   float retval = (float) val;
 #ifndef NO_ERRNO
   if (isinf (retval) && !isinf (val))
-    _REENT->_errno = ERANGE;
+    _REENT_ERRNO(_REENT) = ERANGE;
 #endif
   return retval;
 }
@@ -272,7 +272,7 @@ wcstof (const wchar_t *__restrict nptr,
   float retval = (float) val;
 #ifndef NO_ERRNO
   if (isinf (retval) && !isinf (val))
-    _REENT->_errno = ERANGE;
+    _REENT_ERRNO(_REENT) = ERANGE;
 #endif
 
   return retval;
diff --git a/newlib/libc/stdlib/wcstoimax.c b/newlib/libc/stdlib/wcstoimax.c
index 02ab1c1f4..0c939c978 100644
--- a/newlib/libc/stdlib/wcstoimax.c
+++ b/newlib/libc/stdlib/wcstoimax.c
@@ -122,10 +122,10 @@ _wcstoimax_l(struct _reent *rptr, const wchar_t * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = neg ? INTMAX_MIN : INTMAX_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/wcstol.c b/newlib/libc/stdlib/wcstol.c
index 8b6de3873..2a8c28f97 100644
--- a/newlib/libc/stdlib/wcstol.c
+++ b/newlib/libc/stdlib/wcstol.c
@@ -200,7 +200,7 @@ _wcstol_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_MIN : LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoll.c b/newlib/libc/stdlib/wcstoll.c
index c0e5dc747..34a7e376d 100644
--- a/newlib/libc/stdlib/wcstoll.c
+++ b/newlib/libc/stdlib/wcstoll.c
@@ -200,7 +200,7 @@ _wcstoll_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = neg ? LONG_LONG_MIN : LONG_LONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoul.c b/newlib/libc/stdlib/wcstoul.c
index fe3c87867..9c78fc3b4 100644
--- a/newlib/libc/stdlib/wcstoul.c
+++ b/newlib/libc/stdlib/wcstoul.c
@@ -179,7 +179,7 @@ _wcstoul_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = ULONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoull.c b/newlib/libc/stdlib/wcstoull.c
index 5ac325790..09422fa97 100644
--- a/newlib/libc/stdlib/wcstoull.c
+++ b/newlib/libc/stdlib/wcstoull.c
@@ -150,7 +150,7 @@ _wcstoull_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	register int neg = 0, any, cutlim;
 
 	if(base < 0  ||  base == 1  ||  base > 36)  {
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 		return(0ULL);
 	}
 	/*
@@ -195,7 +195,7 @@ _wcstoull_l (struct _reent *rptr, const wchar_t *nptr, wchar_t **endptr,
 	}
 	if (any < 0) {
 		acc = ULLONG_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != 0)
diff --git a/newlib/libc/stdlib/wcstoumax.c b/newlib/libc/stdlib/wcstoumax.c
index 17b5275fa..623f5b947 100644
--- a/newlib/libc/stdlib/wcstoumax.c
+++ b/newlib/libc/stdlib/wcstoumax.c
@@ -121,10 +121,10 @@ _wcstoumax_l(struct _reent *rptr,const wchar_t * __restrict nptr,
 	}
 	if (any < 0) {
 		acc = UINTMAX_MAX;
-		rptr->_errno = ERANGE;
+		_REENT_ERRNO(rptr) = ERANGE;
 	} else if (!any) {
 noconv:
-		rptr->_errno = EINVAL;
+		_REENT_ERRNO(rptr) = EINVAL;
 	} else if (neg)
 		acc = -acc;
 	if (endptr != NULL)
diff --git a/newlib/libc/stdlib/wctomb_r.c b/newlib/libc/stdlib/wctomb_r.c
index b4799341e..a7f87cd9e 100644
--- a/newlib/libc/stdlib/wctomb_r.c
+++ b/newlib/libc/stdlib/wctomb_r.c
@@ -35,7 +35,7 @@ __ascii_wctomb (struct _reent *r,
   if ((size_t)wchar >= 0x100)
 #endif
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
 
@@ -133,7 +133,7 @@ __utf8_wctomb (struct _reent *r,
       return 4;
     }
 
-  r->_errno = EILSEQ;
+  _REENT_ERRNO(r) = EILSEQ;
   return -1;
 }
 
@@ -165,7 +165,7 @@ __sjis_wctomb (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -204,7 +204,7 @@ __eucjp_wctomb (struct _reent *r,
 	}
       else
 	{
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
@@ -244,7 +244,7 @@ __jis_wctomb (struct _reent *r,
 	  *s = (char)char2;
 	  return cnt + 2;
 	}
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   if (state->__state != 0)
@@ -284,14 +284,14 @@ ___iso_wctomb (struct _reent *r, char *s, wchar_t _wchar, int iso_idx,
 		*s = (char) (mb + 0xa0);
 		return 1;
 	      }
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
  
   if ((size_t)wchar >= 0x100)
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
 
@@ -440,14 +440,14 @@ ___cp_wctomb (struct _reent *r, char *s, wchar_t _wchar, int cp_idx,
 		*s = (char) (mb + 0x80);
 		return 1;
 	      }
-	  r->_errno = EILSEQ;
+	  _REENT_ERRNO(r) = EILSEQ;
 	  return -1;
 	}
     }
 
   if ((size_t)wchar >= 0x100)
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
 
diff --git a/newlib/libc/string/strerror.c b/newlib/libc/string/strerror.c
index c1b934696..8f961d3b5 100644
--- a/newlib/libc/string/strerror.c
+++ b/newlib/libc/string/strerror.c
@@ -882,7 +882,7 @@ _strerror_r (struct _reent *ptr,
 #endif
     default:
       if (!errptr)
-        errptr = &ptr->_errno;
+        errptr = &_REENT_ERRNO(ptr);
       if ((error = _user_strerror (errnum, internal, errptr)) == 0)
         error = "";
       break;
diff --git a/winsup/cygwin/cygerrno.h b/winsup/cygwin/cygerrno.h
index 940bfa5e0..e1c3f04f7 100644
--- a/winsup/cygwin/cygerrno.h
+++ b/winsup/cygwin/cygerrno.h
@@ -36,7 +36,7 @@ extern inline int
 __set_errno (const char *fn, int ln, int val)
 {
   debug_printf ("%s:%d setting errno %d", fn, ln, val);
-  return errno = _impure_ptr->_errno = val;
+  return errno = _REENT_ERRNO(_impure_ptr) = val;
 }
 #define set_errno(val) __set_errno (__PRETTY_FUNCTION__, __LINE__, (val))
 
@@ -55,7 +55,7 @@ class save_errno
     save_errno (int what) {saved = get_errno (); set_errno (what); }
     void set (int what) {set_errno (what); saved = what;}
     void reset () {saved = get_errno ();}
-    ~save_errno () {errno = _impure_ptr->_errno = saved;}
+    ~save_errno () {errno = _REENT_ERRNO(_impure_ptr) = saved;}
   };
 
 extern const char *__sp_fn;
diff --git a/winsup/cygwin/errno.cc b/winsup/cygwin/errno.cc
index f200e5b73..2f2c26984 100644
--- a/winsup/cygwin/errno.cc
+++ b/winsup/cygwin/errno.cc
@@ -335,7 +335,7 @@ void
 seterrno_from_win_error (const char *file, int line, DWORD code)
 {
   syscall_printf ("%s:%d windows error %u", file, line, code);
-  errno = _impure_ptr->_errno =  geterrno_from_win_error (code, EACCES);
+  errno = _REENT_ERRNO(_impure_ptr) =  geterrno_from_win_error (code, EACCES);
 }
 
 int
@@ -353,7 +353,7 @@ seterrno_from_nt_status (const char *file, int line, NTSTATUS status)
   SetLastError (code);
   syscall_printf ("%s:%d status %y -> windows error %u",
 		  file, line, status, code);
-  errno = _impure_ptr->_errno =  geterrno_from_win_error (code, EACCES);
+  errno = _REENT_ERRNO(_impure_ptr) =  geterrno_from_win_error (code, EACCES);
 }
 
 static char *
diff --git a/winsup/cygwin/strfuncs.cc b/winsup/cygwin/strfuncs.cc
index 9a97b3a24..22a79b614 100644
--- a/winsup/cygwin/strfuncs.cc
+++ b/winsup/cygwin/strfuncs.cc
@@ -144,7 +144,7 @@ __db_wctomb (struct _reent *r, char *s, wchar_t wchar, UINT cp)
   if (ret > 0 && !def_used)
     return ret;
 
-  r->_errno = EILSEQ;
+  _REENT_ERRNO(r) = EILSEQ;
   return -1;
 }
 
@@ -194,7 +194,7 @@ __eucjp_wctomb (struct _reent *r, char *s, wchar_t wchar, mbstate_t *state)
       return ret;
     }
 
-  r->_errno = EILSEQ;
+  _REENT_ERRNO(r) = EILSEQ;
   return -1;
 }
 
@@ -255,7 +255,7 @@ __db_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n, UINT cp,
 	 here is to check if the first byte returns a valid value... */
       else if (MultiByteToWideChar (cp, MB_ERR_INVALID_CHARS, s, 1, pwc, 1))
 	return 1;
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   state->__value.__wchb[state->__count] = *s;
@@ -263,7 +263,7 @@ __db_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n, UINT cp,
 			     (const char *) state->__value.__wchb, 2, pwc, 1);
   if (!ret)
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   state->__count = 0;
@@ -324,7 +324,7 @@ __eucjp_mbtowc (struct _reent *r, wchar_t *pwc, const char *s, size_t n,
 	}
       else if (MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS, s, 1, pwc, 1))
 	return 1;
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   state->__value.__wchb[state->__count++] = *s;
@@ -347,7 +347,7 @@ jis_x_0212:
   if (!MultiByteToWideChar (20932, MB_ERR_INVALID_CHARS,
 			    (const char *) state->__value.__wchb, 2, pwc, 1))
     {
-      r->_errno = EILSEQ;
+      _REENT_ERRNO(r) = EILSEQ;
       return -1;
     }
   state->__count = 0;
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index 7d53c2af7..b8260b4d1 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -1900,7 +1900,7 @@ _fstat_r (struct _reent *ptr, int fd, struct stat *buf)
   int ret;
 
   if ((ret = fstat (fd, buf)) == -1)
-    ptr->_errno = get_errno ();
+    _REENT_ERRNO(ptr) = get_errno ();
   return ret;
 }
 
@@ -2051,7 +2051,7 @@ _stat_r (struct _reent *__restrict ptr, const char *__restrict name,
   int ret;
 
   if ((ret = stat (name, buf)) == -1)
-    ptr->_errno = get_errno ();
+    _REENT_ERRNO(ptr) = get_errno ();
   return ret;
 }
 
-- 
2.35.3


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Fw: [PATCH 03/14] Add _REENT_ERRNO(ptr)
  2022-07-13  7:17               ` Sebastian Huber
@ 2022-07-13  7:50                 ` Corinna Vinschen
  2022-07-13  8:19                   ` Sebastian Huber
  0 siblings, 1 reply; 30+ messages in thread
From: Corinna Vinschen @ 2022-07-13  7:50 UTC (permalink / raw)
  To: newlib

On Jul 13 09:17, Sebastian Huber wrote:
> On 12/07/2022 20:25, Corinna Vinschen wrote:
> > > What about renaming the existing __errno_r() uses in Newlib to
> > > _REENT_ERRNO() with a definition of __errno_r() in <sys/errno.h> for
> > > potential users outside of Newlib?
> > Sounds good to me.
> 
> Attached is a new version of this patch. In this patch the uses of
> __errno_r() were replaced with _REENT_ERRNO().

In future, would you mind to send followup patches as v2, v3, the same
way as the first patch, please?  It's not required as such, but it makes
applying a series easier.

Having said that, the patchset looks good to me.  AFAICS this also
uncovered a minor bug in Cygwin in the errno handling, but that's
something for us Cygwin folks to patch after you applied your set.


Thanks,
Corinna


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Fw: [PATCH 03/14] Add _REENT_ERRNO(ptr)
  2022-07-13  7:50                 ` Corinna Vinschen
@ 2022-07-13  8:19                   ` Sebastian Huber
  2022-07-13 11:13                     ` Corinna Vinschen
  0 siblings, 1 reply; 30+ messages in thread
From: Sebastian Huber @ 2022-07-13  8:19 UTC (permalink / raw)
  To: newlib

On 13/07/2022 09:50, Corinna Vinschen wrote:
> On Jul 13 09:17, Sebastian Huber wrote:
>> On 12/07/2022 20:25, Corinna Vinschen wrote:
>>>> What about renaming the existing __errno_r() uses in Newlib to
>>>> _REENT_ERRNO() with a definition of __errno_r() in <sys/errno.h> for
>>>> potential users outside of Newlib?
>>> Sounds good to me.
>> Attached is a new version of this patch. In this patch the uses of
>> __errno_r() were replaced with _REENT_ERRNO().
> In future, would you mind to send followup patches as v2, v3, the same
> way as the first patch, please?  It's not required as such, but it makes
> applying a series easier.

Ok, sorry.

> 
> Having said that, the patchset looks good to me.  AFAICS this also
> uncovered a minor bug in Cygwin in the errno handling, but that's
> something for us Cygwin folks to patch after you applied your set.

Thanks for the review, I checked it in.

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: Fw: [PATCH 03/14] Add _REENT_ERRNO(ptr)
  2022-07-13  8:19                   ` Sebastian Huber
@ 2022-07-13 11:13                     ` Corinna Vinschen
  0 siblings, 0 replies; 30+ messages in thread
From: Corinna Vinschen @ 2022-07-13 11:13 UTC (permalink / raw)
  To: newlib

On Jul 13 10:19, Sebastian Huber wrote:
> On 13/07/2022 09:50, Corinna Vinschen wrote:
> > On Jul 13 09:17, Sebastian Huber wrote:
> > > On 12/07/2022 20:25, Corinna Vinschen wrote:
> > > > > What about renaming the existing __errno_r() uses in Newlib to
> > > > > _REENT_ERRNO() with a definition of __errno_r() in <sys/errno.h> for
> > > > > potential users outside of Newlib?
> > > > Sounds good to me.
> > > Attached is a new version of this patch. In this patch the uses of
> > > __errno_r() were replaced with _REENT_ERRNO().
> > In future, would you mind to send followup patches as v2, v3, the same
> > way as the first patch, please?  It's not required as such, but it makes
> > applying a series easier.
> 
> Ok, sorry.
> 
> > 
> > Having said that, the patchset looks good to me.  AFAICS this also
> > uncovered a minor bug in Cygwin in the errno handling, but that's
> > something for us Cygwin folks to patch after you applied your set.
> 
> Thanks for the review, I checked it in.

Thanks.  To close this, the alleged bug in Cygwin's errno handling isn't
a bug, just a misunderstanding on my side.


Corinna


^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2022-07-13 11:13 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-21 12:49 [PATCH 00/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
2022-06-21 12:49 ` [PATCH 01/14] Move content in <sys/reent.h> Sebastian Huber
2022-06-21 12:49 ` [PATCH 02/14] Define _REENT_EMERGENCY(ptr) only once Sebastian Huber
2022-06-21 12:49 ` [PATCH 03/14] Add _REENT_ERRNO(ptr) Sebastian Huber
     [not found]   ` <BN2P110MB15447CD8C6DD815779AA7E4C9AB39@BN2P110MB1544.NAMP110.PROD.OUTLOOK.COM>
2022-06-21 14:41     ` Fw: " C Howland
2022-06-23 10:55       ` Sebastian Huber
2022-07-11  7:41         ` Sebastian Huber
2022-07-12 16:11         ` Corinna Vinschen
2022-07-12 16:38           ` Sebastian Huber
2022-07-12 18:25             ` Corinna Vinschen
2022-07-13  7:17               ` Sebastian Huber
2022-07-13  7:50                 ` Corinna Vinschen
2022-07-13  8:19                   ` Sebastian Huber
2022-07-13 11:13                     ` Corinna Vinschen
2022-06-21 12:49 ` [PATCH 04/14] Add _REENT_STDIN(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 05/14] Add _REENT_STDOUT(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 06/14] Add _REENT_STDERR(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 07/14] Add _REENT_INC(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 08/14] Add _REENT_LOCALE(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 09/14] Add _REENT_CLEANUP(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 10/14] Add _REENT_CVTLEN(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 11/14] Add _REENT_CVTBUF(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 12/14] Add _REENT_SIG_FUNC(ptr) Sebastian Huber
2022-06-21 12:49 ` [PATCH 13/14] Add _REENT_IS_NULL() Sebastian Huber
2022-06-21 12:49 ` [PATCH 14/14] Add --enable-newlib-reent-thread-local option Sebastian Huber
2022-06-21 13:59   ` Torbjorn SVENSSON
2022-06-21 15:10     ` Sebastian Huber
2022-07-12 11:18 ` [PATCH 00/14] " Sebastian Huber
2022-07-12 14:45   ` Corinna Vinschen
2022-07-12 15:53     ` Sebastian Huber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).