From 1235d276b78a769bded01d51c9bf3cdc480db9fb Mon Sep 17 00:00:00 2001 From: Brooks Davis Date: Fri, 27 Sep 2024 20:27:46 +0100 Subject: [PATCH] lib{c,sys}: stop exposing errno symbol Officially since C11 (and in reality FreeBSD since 3.0 with commit 1b46cb523df3) errno has been defined to be a macro. Rename the symbol to __libsys_errno and move it to FBSDprivate_1.0 and confine it entierly to libsys for use by libthr. Add a FBSD_1.0 compat symbol for existing binaries that were incorrectly linked to the errno symbol during libc.so.7's lifetime. This deliberately breaks linking software that directly links to errno. Such software is broken and will fail in surprising ways if it becomes threaded (e.g., if it triggers loading of a pam or nss module that uses threads.) Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D46780 --- lib/libsys/Makefile | 2 ++ lib/libsys/Symbol.map | 1 + lib/libsys/Symbol.sys.map | 1 - lib/libsys/__error.c | 10 +++++----- lib/libthr/sys/thr_error.c | 5 ++--- lib/libthr/thread/thr_rtld.c | 7 +++---- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/libsys/Makefile b/lib/libsys/Makefile index b4c0e91e860..b4b4662ae47 100644 --- a/lib/libsys/Makefile +++ b/lib/libsys/Makefile @@ -25,6 +25,8 @@ MK_SSP= no INCS= libsys.h _libsys.h +CFLAGS+=-DLIBSYS + CFLAGS+=-I${LIBSYS_SRCTOP}/include -I${LIBC_SRCTOP}/include CFLAGS+=-I${LIBSYS_SRCTOP}/${LIBC_ARCH} CFLAGS+=-I${LIBC_SRCTOP}/${LIBC_ARCH} diff --git a/lib/libsys/Symbol.map b/lib/libsys/Symbol.map index b2bc69108bb..51ea1b5da23 100644 --- a/lib/libsys/Symbol.map +++ b/lib/libsys/Symbol.map @@ -1,5 +1,6 @@ FBSDprivate_1.0 { __elf_aux_vector; + __libsys_errno; __getosreldate; __libsys_interposing_slot; __realpathat; diff --git a/lib/libsys/Symbol.sys.map b/lib/libsys/Symbol.sys.map index 5a4e66baf3c..85373b1f9cd 100644 --- a/lib/libsys/Symbol.sys.map +++ b/lib/libsys/Symbol.sys.map @@ -72,7 +72,6 @@ FBSD_1.0 { extattr_set_file; extattr_set_link; extattrctl; - errno; fchdir; fchflags; fchmod; diff --git a/lib/libsys/__error.c b/lib/libsys/__error.c index 1132bdba9c6..41016e9a1ed 100644 --- a/lib/libsys/__error.c +++ b/lib/libsys/__error.c @@ -31,13 +31,15 @@ #include "libc_private.h" -int errno; +int __libsys_errno; +#ifdef LIBSYS +__sym_compat(errno, __libsys_errno, FBSD_1.0); +#endif static int * __error_unthreaded(void) { - - return (&errno); + return (&__libsys_errno); } static int *(*__error_selector)(void) = __error_unthreaded; @@ -45,13 +47,11 @@ static int *(*__error_selector)(void) = __error_unthreaded; void __set_error_selector(int *(*arg)(void)) { - __error_selector = arg; } int * __error(void) { - return (__error_selector()); } diff --git a/lib/libthr/sys/thr_error.c b/lib/libthr/sys/thr_error.c index 7ce3a84fab6..ec7a57bf661 100644 --- a/lib/libthr/sys/thr_error.c +++ b/lib/libthr/sys/thr_error.c @@ -39,8 +39,7 @@ #include "libc_private.h" #include "thr_private.h" -#undef errno -extern int errno; +extern int __libsys_errno; __weak_reference(__error_threaded, __error); int * @@ -53,5 +52,5 @@ __error_threaded(void) if (curthread != NULL && curthread != _thr_initial) return (&curthread->error); } - return (&errno); + return (&__libsys_errno); } diff --git a/lib/libthr/thread/thr_rtld.c b/lib/libthr/thread/thr_rtld.c index 68a02e9aca1..e5a7f86de28 100644 --- a/lib/libthr/thread/thr_rtld.c +++ b/lib/libthr/thread/thr_rtld.c @@ -39,8 +39,7 @@ #include "rtld_lock.h" #include "thr_private.h" -#undef errno -extern int errno; +extern int __libsys_errno; static int _thr_rtld_clr_flag(int); static void *_thr_rtld_lock_create(void); @@ -96,14 +95,14 @@ _thr_rtld_lock_destroy(void *lock) if (curthread != _thr_initial) \ errsave = curthread->error; \ else \ - errsave = errno; \ + errsave = __libsys_errno; \ } #define RESTORE_ERRNO() { \ if (curthread != _thr_initial) \ curthread->error = errsave; \ else \ - errno = errsave; \ + __libsys_errno = errsave; \ } static void