[PATCH 3/5] newlib: prepare to eliminate unused TLS context arguments
H. Peter Anvin
hpa@zytor.com
Thu Dec 3 22:01:37 GMT 2026
Remove pointer-only operations on reentrancy context arguments when
TLS supplies the state. Do not mix the unused pointer into temporary
file names, and drop the restrict qualifier from the context parameter.
Introduce __NULL_REENT_PTR and use it wherever a null context is passed.
Keep it defined as NULL for now so this patch preserves the pointer ABI.
Use reentrancy access macros in libgloss instead of dereferencing the
context directly.
These changes allow a later patch to replace the unused TLS pointer
with an empty argument type without changing call sites again.
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
---
libgloss/or1k/impure.c | 12 ++++++------
libgloss/or1k/syscalls.c | 26 +++++++++++++-------------
libgloss/or1k/util.c | 12 ++++++------
libgloss/rs6000/sim-errno.c | 2 +-
newlib/libc/include/sys/reent.h | 10 ++++++----
newlib/libc/stdio/asnprintf.c | 2 +-
newlib/libc/stdio/findfp.c | 4 ++--
newlib/libc/stdio/tmpnam.c | 6 +++++-
8 files changed, 40 insertions(+), 34 deletions(-)
diff --git a/libgloss/or1k/impure.c b/libgloss/or1k/impure.c
index 7780c9170242..335b7ec7d150 100644
--- a/libgloss/or1k/impure.c
+++ b/libgloss/or1k/impure.c
@@ -70,19 +70,19 @@ _or1k_libc_impure_init (void)
#ifdef __OR1K_MULTICORE__
uint32_t c;
- _or1k_impure_ptr = _sbrk_r(0, sizeof(struct _reent*) * or1k_numcores());
- _or1k_exception_impure_ptr = _sbrk_r(0, sizeof(struct _reent*) * or1k_numcores());
- _or1k_current_impure_ptr = _sbrk_r(0, sizeof(struct _reent*) * or1k_numcores());
+ _or1k_impure_ptr = _sbrk_r(__NULL_REENT_PTR, sizeof(struct _reent*) * or1k_numcores());
+ _or1k_exception_impure_ptr = _sbrk_r(__NULL_REENT_PTR, sizeof(struct _reent*) * or1k_numcores());
+ _or1k_current_impure_ptr = _sbrk_r(__NULL_REENT_PTR, sizeof(struct _reent*) * or1k_numcores());
_or1k_impure_ptr[0] = _impure_ptr;
_REENT_INIT_PTR(_impure_ptr);
for (c = 1; c < or1k_numcores(); c++) {
- _or1k_impure_ptr[c] = _sbrk_r(0, sizeof(struct _reent));
+ _or1k_impure_ptr[c] = _sbrk_r(__NULL_REENT_PTR, sizeof(struct _reent));
_REENT_INIT_PTR(_or1k_impure_ptr[c]);
}
for (c = 0; c < or1k_numcores(); c++) {
- _or1k_exception_impure_ptr[c] = _sbrk_r(0, sizeof(struct _reent));
+ _or1k_exception_impure_ptr[c] = _sbrk_r(__NULL_REENT_PTR, sizeof(struct _reent));
_REENT_INIT_PTR(_or1k_exception_impure_ptr[c]);
}
@@ -118,6 +118,6 @@ _or1k_reent_init(void)
{
#ifdef __OR1K_MULTICORE__
size_t memsize = sizeof(struct _or1k_reent) * or1k_numcores();
- _or1k_reent = (struct _or1k_reent*) _sbrk_r(0, memsize);
+ _or1k_reent = (struct _or1k_reent*) _sbrk_r(__NULL_REENT_PTR, memsize);
#endif
}
diff --git a/libgloss/or1k/syscalls.c b/libgloss/or1k/syscalls.c
index fcd4b681c772..5e6a993d8395 100644
--- a/libgloss/or1k/syscalls.c
+++ b/libgloss/or1k/syscalls.c
@@ -52,7 +52,7 @@ _exit(int rc)
int
_close_r(__reent_ptr_t reent, int fildes)
{
- reent->_errno = ENOSYS;
+ _REENT_ERRNO(reent) = ENOSYS;
return -1;
}
@@ -60,7 +60,7 @@ int
_execve_r(__reent_ptr_t reent, const char *name, char * const *argv,
char * const *env)
{
- reent->_errno = ENOSYS;
+ _REENT_ERRNO(reent) = ENOSYS;
return -1;
}
@@ -74,42 +74,42 @@ _fork_r(__reent_ptr_t reent)
int
_fstat_r(__reent_ptr_t reent, int fildes, struct stat *st)
{
- reent->_errno = ENOSYS;
+ _REENT_ERRNO(reent) = ENOSYS;
return -1;
}
int
_getpid_r(__reent_ptr_t reent)
{
- reent->_errno = ENOSYS;
+ _REENT_ERRNO(reent) = ENOSYS;
return -1;
}
int
_gettimeofday(__reent_ptr_t reent, struct timeval *ptimeval, void *ptimezone)
{
- reent->_errno = ENOSYS;
+ _REENT_ERRNO(reent) = ENOSYS;
return -1;
}
int
_isatty_r(__reent_ptr_t reent, int file)
{
- reent->_errno = ENOSYS;
+ _REENT_ERRNO(reent) = ENOSYS;
return 0;
}
int
_kill_r(__reent_ptr_t reent, int pid, int sig)
{
- reent->_errno = ENOSYS;
+ _REENT_ERRNO(reent) = ENOSYS;
return -1;
}
int
_link_r(__reent_ptr_t reent, const char *existing, const char *new)
{
- reent->_errno = ENOSYS;
+ _REENT_ERRNO(reent) = ENOSYS;
return -1;
}
@@ -123,35 +123,35 @@ _lseek_r(__reent_ptr_t reent, int file, _off_t ptr, int dir)
int
_open_r(__reent_ptr_t reent, const char *file, int flags, int mode)
{
- reent->_errno = ENOSYS;
+ _REENT_ERRNO(reent) = ENOSYS;
return -1;
}
_ssize_t
_read_r(__reent_ptr_t reent, int file, void *ptr, size_t len)
{
- reent->_errno = ENOSYS;
+ _REENT_ERRNO(reent) = ENOSYS;
return -1;
}
int
_readlink_r(__reent_ptr_t reent, const char *path, char *buf, size_t bufsize)
{
- reent->_errno = ENOSYS;
+ _REENT_ERRNO(reent) = ENOSYS;
return -1;
}
int
_stat_r(__reent_ptr_t reent, const char *path, struct stat *buf)
{
- reent->_errno = EIO;
+ _REENT_ERRNO(reent) = EIO;
return -1;
}
int
_unlink_r(__reent_ptr_t reent, const char * path)
{
- reent->_errno = EIO;
+ _REENT_ERRNO(reent) = EIO;
return (-1);
}
diff --git a/libgloss/or1k/util.c b/libgloss/or1k/util.c
index d2ee84d569af..4f6ddea9a040 100644
--- a/libgloss/or1k/util.c
+++ b/libgloss/or1k/util.c
@@ -43,8 +43,8 @@ void _or1k_init() {
uint32_t c;
// Initialize stacks
- _or1k_stack_core = _sbrk_r(0, sizeof(uint8_t*) * or1k_numcores());
- _or1k_exception_stack_core = _sbrk_r(0, sizeof(uint8_t*) * or1k_numcores());
+ _or1k_stack_core = _sbrk_r(__NULL_REENT_PTR, sizeof(uint8_t*) * or1k_numcores());
+ _or1k_exception_stack_core = _sbrk_r(__NULL_REENT_PTR, sizeof(uint8_t*) * or1k_numcores());
_or1k_stack_core[0] = _or1k_stack_top;
_or1k_exception_stack_core[0] = _or1k_exception_stack_top;
@@ -56,12 +56,12 @@ void _or1k_init() {
}
size_t exc_size = sizeof(void*) * or1k_numcores() * OR1K_NUM_EXCEPTIONS;
- _or1k_exception_handler_table = _sbrk_r(0, exc_size);
+ _or1k_exception_handler_table = _sbrk_r(__NULL_REENT_PTR, exc_size);
size_t int_size = sizeof(void*) * or1k_numcores() * 32;
size_t intdata_size = sizeof(void*) * or1k_numcores() * 32;
- _or1k_interrupt_handler_table = _sbrk_r(0, int_size);
- _or1k_interrupt_handler_data_ptr_table = _sbrk_r(0, intdata_size);
+ _or1k_interrupt_handler_table = _sbrk_r(__NULL_REENT_PTR, int_size);
+ _or1k_interrupt_handler_data_ptr_table = _sbrk_r(__NULL_REENT_PTR, intdata_size);
#endif
_or1k_reent_init();
@@ -75,7 +75,7 @@ void _or1k_init() {
#endif
#ifdef __OR1K_MULTICORE__
- _or1k_exception_level = _sbrk_r(0, 4 * or1k_numcores());
+ _or1k_exception_level = _sbrk_r(__NULL_REENT_PTR, 4 * or1k_numcores());
for (c = 0; c < or1k_numcores(); c++) {
_or1k_exception_level[c] = 0;
}
diff --git a/libgloss/rs6000/sim-errno.c b/libgloss/rs6000/sim-errno.c
index 29cb4a2be624..c29af83de75a 100644
--- a/libgloss/rs6000/sim-errno.c
+++ b/libgloss/rs6000/sim-errno.c
@@ -23,6 +23,6 @@ int
_cerror (e)
int e;
{
- _REENT->_errno = e;
+ _REENT_ERRNO(_REENT) = e;
return -1;
}
diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 764d96e4f344..bbe953c5a993 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -43,6 +43,8 @@ typedef __uint32_t __ULong;
struct _reent;
+#define __NULL_REENT_PTR NULL
+
struct __locale_t;
/*
@@ -827,12 +829,12 @@ extern struct _reent _impure_data __ATTRIBUTE_IMPURE_DATA__;
#else /* _REENT_THREAD_LOCAL */
-#define _REENT NULL
-#define _GLOBAL_REENT NULL
+#define _REENT __NULL_REENT_PTR
+#define _GLOBAL_REENT __NULL_REENT_PTR
/*
- * Since _REENT is defined as NULL, this macro ensures that calls to
- * CHECK_INIT() do not automatically fail.
+ * Thread-local state needs no reentrancy pointer, so CHECK_INIT() must
+ * not treat the placeholder argument as an uninitialized context.
*/
#define _REENT_IS_NULL(_ptr) 0
diff --git a/newlib/libc/stdio/asnprintf.c b/newlib/libc/stdio/asnprintf.c
index cacd444bb70f..3cbdaf9f8d04 100644
--- a/newlib/libc/stdio/asnprintf.c
+++ b/newlib/libc/stdio/asnprintf.c
@@ -14,7 +14,7 @@
#include "local.h"
char *
-_asnprintf_r (__reent_ptr_t __restrict ptr,
+_asnprintf_r (__reent_ptr_t ptr,
char *buf,
size_t *lenp,
const char *__restrict fmt, ...)
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c
index e3dfeb8de0f3..fa163c141ff8 100644
--- a/newlib/libc/stdio/findfp.c
+++ b/newlib/libc/stdio/findfp.c
@@ -299,13 +299,13 @@ void
__fp_lock_all (void)
{
__sfp_lock_acquire ();
- (void) _fwalk_sglue (NULL, __fp_lock, &__sglue);
+ (void) _fwalk_sglue (__NULL_REENT_PTR, __fp_lock, &__sglue);
}
void
__fp_unlock_all (void)
{
- (void) _fwalk_sglue (NULL, __fp_unlock, &__sglue);
+ (void) _fwalk_sglue (__NULL_REENT_PTR, __fp_unlock, &__sglue);
__sfp_lock_release ();
}
#endif
diff --git a/newlib/libc/stdio/tmpnam.c b/newlib/libc/stdio/tmpnam.c
index 2846479aa845..88f1ae94566b 100644
--- a/newlib/libc/stdio/tmpnam.c
+++ b/newlib/libc/stdio/tmpnam.c
@@ -166,8 +166,12 @@ _tempnam_r (__reent_ptr_t p,
filename = _malloc_r (p, length);
if (filename)
{
+ int pid = _getpid_r (p);
+#ifndef _REENT_THREAD_LOCAL
+ pid ^= (int) (_POINTER_INT) p;
+#endif
if (! worker (p, filename, dir, prefix,
- _getpid_r (p) ^ (int) (_POINTER_INT) p, &_REENT_INC(p)))
+ pid, &_REENT_INC(p)))
return NULL;
}
return filename;
--
2.56.1
More information about the Newlib
mailing list