diff --git a/lib/libsys/Makefile b/lib/libsys/Makefile index b4c0e91e860c..b4b4662ae47f 100644 --- a/lib/libsys/Makefile +++ b/lib/libsys/Makefile @@ -1,78 +1,80 @@ PACKAGE= clibs SHLIBDIR?= /lib .include LIBC_SRCTOP?= ${.CURDIR}/../libc LIBSYS_SRCTOP?= ${.CURDIR} # Pick the current architecture directory for libsys. In general, this is named # MACHINE_CPUARCH, but some ABIs are different enough to require their own # libsys, so allow a directory named MACHINE_ARCH to override this (though # treat powerpc64le and powerpc64 the same). # Note: This is copied from libc/Makefile M=${MACHINE_ARCH:S/powerpc64le/powerpc64/} .if exists(${LIBC_SRCTOP}/${M}) LIBC_ARCH=${M} .else LIBC_ARCH=${MACHINE_CPUARCH} .endif LIB=sys SHLIB_MAJOR= 7 WARNS?= 2 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} .PATH: ${LIBC_SRCTOP}/string SRCS+= memcpy.c memset.c strlcpy.c CLEANFILES+=tags INSTALL_PIC_ARCHIVE= #XXX? BUILD_NOSSP_PIC_ARCHIVE= PRECIOUSLIB= # Use a more efficient TLS model for libc since we can reasonably assume that # it will be loaded during program startup. CFLAGS+= -ftls-model=initial-exec # # Link with static libcompiler_rt.a. # LDFLAGS+= -nodefaultlibs LDFLAGS+= -Wl,-Bsymbolic LIBADD+= compiler_rt .if ${MK_SSP} != "no" && \ (${LIBC_ARCH} == "i386" || ${LIBC_ARCH:Mpowerpc*} != "") LIBADD+= ssp_nonshared .endif # Define (empty) variables so that make doesn't give substitution # errors if the included makefiles don't change these: MDASM= MIASM= NOASM= SYM_MAPS+= ${LIBSYS_SRCTOP}/Symbol.map SRCS+= auxv.c \ interposing_table.c .include "${LIBSYS_SRCTOP}/Makefile.sys" SYM_MAPS+= ${LIBSYS_SRCTOP}/Symbol.thr.map .PATH: ${LIBSYS_SRCTOP}/${MACHINE_CPUARCH} .sinclude "${LIBSYS_SRCTOP}/${MACHINE_CPUARCH}/Makefile.thr" .if !${SRCS:M_umtx_op_err.S} SRCS+=_umtx_op_err.c .endif VERSION_DEF=${LIBC_SRCTOP}/Versions.def SYMBOL_MAPS=${SYM_MAPS} .include diff --git a/lib/libsys/Symbol.map b/lib/libsys/Symbol.map index b2bc69108bbb..51ea1b5da231 100644 --- a/lib/libsys/Symbol.map +++ b/lib/libsys/Symbol.map @@ -1,13 +1,14 @@ FBSDprivate_1.0 { __elf_aux_vector; + __libsys_errno; __getosreldate; __libsys_interposing_slot; __realpathat; _elf_aux_info; freebsd11_fstat; freebsd11_fstatat; freebsd11_getfsstat; freebsd11_lstat; freebsd11_stat; freebsd11_statfs; }; diff --git a/lib/libsys/Symbol.sys.map b/lib/libsys/Symbol.sys.map index 5a4e66baf3c4..85373b1f9cda 100644 --- a/lib/libsys/Symbol.sys.map +++ b/lib/libsys/Symbol.sys.map @@ -1,393 +1,392 @@ /* * It'd be nice to automatically generate the syscall symbols, but we * don't know to what version they will eventually belong to, so for now * it has to be manual. */ FBSD_1.0 { __acl_aclcheck_fd; __acl_aclcheck_file; __acl_aclcheck_link; __acl_delete_fd; __acl_delete_file; __acl_delete_link; __acl_get_fd; __acl_get_file; __acl_get_link; __acl_set_fd; __acl_set_file; __acl_set_link; __getcwd; __mac_execve; __mac_get_fd; __mac_get_file; __mac_get_link; __mac_get_pid; __mac_get_proc; __mac_set_fd; __mac_set_file; __mac_set_link; __mac_set_proc; __setugid; __syscall; __sysctl; _exit; _umtx_op; abort2; access; acct; adjtime; aio_cancel; aio_error; aio_fsync; aio_read; aio_return; aio_waitcomplete; aio_write; audit; auditctl; auditon; bind; chdir; chflags; chmod; chown; chroot; clock_getres; clock_gettime; clock_settime; dup; dup2; eaccess; execve; extattr_delete_fd; extattr_delete_file; extattr_delete_link; extattr_get_fd; extattr_get_file; extattr_get_link; extattr_list_fd; extattr_list_file; extattr_list_link; extattr_set_fd; extattr_set_file; extattr_set_link; extattrctl; - errno; fchdir; fchflags; fchmod; fchown; fhopen; flock; fpathconf; futimes; getaudit; getaudit_addr; getauid; getcontext; getdtablesize; getegid; geteuid; getfh; getgid; getgroups; getitimer; getpagesize; getpeername; getpgid; getpgrp; getpid; getppid; getpriority; getresgid; getresuid; getrlimit; getrusage; getsid; getsockname; getsockopt; gettimeofday; getuid; ioctl; issetugid; jail; jail_attach; kenv; kill; kldfind; kldfirstmod; kldload; kldnext; kldstat; kldsym; kldunload; kldunloadf; kqueue; kmq_notify; /* Do we want these to be public interfaces? */ kmq_open; /* librt uses them to provide mq_xxx. */ kmq_setattr; kmq_timedreceive; kmq_timedsend; kmq_unlink; ksem_close; ksem_destroy; ksem_getvalue; ksem_init; ksem_open; ksem_post; ksem_timedwait; ksem_trywait; ksem_unlink; ksem_wait; ktrace; lchflags; lchmod; lchown; lgetfh; link; lio_listio; listen; lutimes; mac_syscall; madvise; mincore; minherit; mkdir; mkfifo; mlock; mlockall; modfind; modfnext; modnext; modstat; mount; mprotect; msgget; msgrcv; msgsnd; msgsys; munlock; munlockall; munmap; nfssvc; nmount; ntp_adjtime; ntp_gettime; pathconf; posix_openpt; preadv; profil; pwritev; quotactl; readlink; reboot; rename; revoke; rfork; rmdir; rtprio; rtprio_thread; sched_get_priority_max; sched_get_priority_min; sched_getparam; sched_getscheduler; sched_rr_get_interval; sched_setparam; sched_setscheduler; sched_yield; semget; semop; semsys; sendfile; setaudit; setaudit_addr; setauid; setegid; seteuid; setgid; setgroups; setitimer; setlogin; setpgid; setpriority; setregid; setresgid; setresuid; setreuid; setrlimit; setsid; setsockopt; settimeofday; setuid; shm_unlink; shmat; shmdt; shmget; shmsys; shutdown; sigaltstack; sigpending; sigqueue; sigreturn; socket; socketpair; swapon; symlink; sync; sysarch; syscall; thr_create; thr_exit; thr_kill; thr_kill2; thr_new; thr_self; thr_set_name; thr_suspend; thr_wake; ktimer_create; /* Do we want these to be public interfaces? */ ktimer_delete; /* librt uses them to provide timer_xxx. */ ktimer_getoverrun; ktimer_gettime; ktimer_settime; umask; undelete; unlink; unmount; utimes; utrace; uuidgen; vfork; __error; ftruncate; lseek; mmap; pread; pwrite; truncate; }; FBSD_1.1 { __semctl; cpuset; cpuset_getid; cpuset_setid; cpuset_getaffinity; cpuset_setaffinity; faccessat; fchmodat; fchownat; fexecve; futimesat; jail_get; jail_set; jail_remove; linkat; lpathconf; mkdirat; mkfifoat; msgctl; readlinkat; renameat; setfib; shmctl; symlinkat; unlinkat; }; FBSD_1.2 { cap_enter; cap_getmode; getloginclass; getpagesizes; pdgetpid; pdkill; posix_fallocate; rctl_get_racct; rctl_get_rules; rctl_get_limits; rctl_add_rule; rctl_remove_rule; setloginclass; }; FBSD_1.3 { aio_mlock; bindat; cap_fcntls_get; cap_fcntls_limit; cap_ioctls_get; cap_ioctls_limit; __cap_rights_get; cap_rights_limit; chflagsat; clock_getcpuclockid2; connectat; ffclock_getcounter; ffclock_getestimate; ffclock_setestimate; pipe2; posix_fadvise; procctl; }; FBSD_1.4 { futimens; utimensat; }; FBSD_1.5 { elf_aux_info; fhstat; fhstatfs; fstat; fstatat; fstatfs; getdirentries; getfsstat; getrandom; mknodat; statfs; cpuset_getdomain; cpuset_setdomain; }; FBSD_1.6 { __sysctlbyname; aio_readv; aio_writev; close_range; copy_file_range; fhlink; fhlinkat; fhreadlink; getfhat; funlinkat; shm_rename; }; FBSD_1.7 { fspacectl; kqueuex; membarrier; sched_getcpu; swapoff; timerfd_create; timerfd_gettime; timerfd_settime; }; FBSD_1.8 { getrlimitusage; kcmp; }; FBSDprivate_1.0 { /* Add entries in sort(1) order */ __set_error_selector; __sigwait; gssd_syscall; nlm_syscall; rpctls_syscall; }; diff --git a/lib/libsys/__error.c b/lib/libsys/__error.c index 1132bdba9c65..41016e9a1ed2 100644 --- a/lib/libsys/__error.c +++ b/lib/libsys/__error.c @@ -1,57 +1,57 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1997 John Birrell . * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #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; 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 7ce3a84fab6b..ec7a57bf6610 100644 --- a/lib/libthr/sys/thr_error.c +++ b/lib/libthr/sys/thr_error.c @@ -1,57 +1,56 @@ /*- * SPDX-License-Identifier: BSD-4-Clause * * Copyright (c) 1995 John Birrell . * Copyright (c) 1994 by Chris Provenzano, proven@mit.edu * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Birrell * and Chris Provenzano. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include "libc_private.h" #include "thr_private.h" -#undef errno -extern int errno; +extern int __libsys_errno; __weak_reference(__error_threaded, __error); int * __error_threaded(void) { struct pthread *curthread; if (_thr_initial != NULL) { curthread = _get_curthread(); 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 68a02e9aca1b..e5a7f86de288 100644 --- a/lib/libthr/thread/thr_rtld.c +++ b/lib/libthr/thread/thr_rtld.c @@ -1,286 +1,285 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2006, David Xu * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice unmodified, this list of conditions, and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * A lockless rwlock for rtld. */ #include #include #include #include #include #include "libc_private.h" #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); static void _thr_rtld_lock_destroy(void *); static void _thr_rtld_lock_release(void *); static void _thr_rtld_rlock_acquire(void *); static int _thr_rtld_set_flag(int); static void _thr_rtld_wlock_acquire(void *); struct rtld_lock { struct urwlock lock; char _pad[CACHE_LINE_SIZE - sizeof(struct urwlock)]; }; static struct rtld_lock lock_place[MAX_RTLD_LOCKS] __aligned(CACHE_LINE_SIZE); static int busy_places; static void * _thr_rtld_lock_create(void) { int locki; struct rtld_lock *l; static const char fail[] = "_thr_rtld_lock_create failed\n"; for (locki = 0; locki < MAX_RTLD_LOCKS; locki++) { if ((busy_places & (1 << locki)) == 0) break; } if (locki == MAX_RTLD_LOCKS) { write(2, fail, sizeof(fail) - 1); return (NULL); } busy_places |= (1 << locki); l = &lock_place[locki]; l->lock.rw_flags = URWLOCK_PREFER_READER; return (l); } static void _thr_rtld_lock_destroy(void *lock) { int locki; size_t i; locki = (struct rtld_lock *)lock - &lock_place[0]; for (i = 0; i < sizeof(struct rtld_lock); ++i) ((char *)lock)[i] = 0; busy_places &= ~(1 << locki); } #define SAVE_ERRNO() { \ 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 _thr_rtld_rlock_acquire(void *lock) { struct pthread *curthread; struct rtld_lock *l; int errsave; curthread = _get_curthread(); SAVE_ERRNO(); l = (struct rtld_lock *)lock; THR_CRITICAL_ENTER(curthread); while (_thr_rwlock_rdlock(&l->lock, 0, NULL) != 0) ; curthread->rdlock_count++; RESTORE_ERRNO(); } static void _thr_rtld_wlock_acquire(void *lock) { struct pthread *curthread; struct rtld_lock *l; int errsave; curthread = _get_curthread(); SAVE_ERRNO(); l = (struct rtld_lock *)lock; THR_CRITICAL_ENTER(curthread); while (_thr_rwlock_wrlock(&l->lock, NULL) != 0) ; RESTORE_ERRNO(); } static void _thr_rtld_lock_release(void *lock) { struct pthread *curthread; struct rtld_lock *l; int32_t state; int errsave; curthread = _get_curthread(); SAVE_ERRNO(); l = (struct rtld_lock *)lock; state = l->lock.rw_state; if (__predict_false(_thr_after_fork)) { /* * After fork, only this thread is running, there is no * waiters. Keeping waiters recorded in rwlock breaks * wake logic. */ atomic_clear_int(&l->lock.rw_state, URWLOCK_WRITE_WAITERS | URWLOCK_READ_WAITERS); l->lock.rw_blocked_readers = 0; l->lock.rw_blocked_writers = 0; } if (_thr_rwlock_unlock(&l->lock) == 0) { if ((state & URWLOCK_WRITE_OWNER) == 0) curthread->rdlock_count--; THR_CRITICAL_LEAVE(curthread); } RESTORE_ERRNO(); } static int _thr_rtld_set_flag(int mask __unused) { /* * The caller's code in rtld-elf is broken, it is not signal safe, * just return zero to fool it. */ return (0); } static int _thr_rtld_clr_flag(int mask __unused) { return (0); } /* * ABI bug workaround: This symbol must be present for rtld to accept * RTLI_VERSION from RtldLockInfo */ extern char _pli_rtli_version; char _pli_rtli_version; static char * _thr_dlerror_loc(void) { struct pthread *curthread; curthread = _get_curthread(); return (curthread->dlerror_msg); } static int * _thr_dlerror_seen(void) { struct pthread *curthread; curthread = _get_curthread(); return (&curthread->dlerror_seen); } void _thr_rtld_init(void) { struct RtldLockInfo li; struct pthread *curthread; ucontext_t *uc; int uc_len; char dummy[2] = {}; curthread = _get_curthread(); /* force to resolve _umtx_op PLT */ _umtx_op_err(&dummy, UMTX_OP_WAKE, 1, 0, 0); /* force to resolve errno() PLT */ __error(); /* force to resolve memcpy PLT */ memcpy(&dummy[0], &dummy[1], 1); mprotect(NULL, 0, 0); _rtld_get_stack_prot(); thr_wake(-1); li.rtli_version = RTLI_VERSION; li.lock_create = _thr_rtld_lock_create; li.lock_destroy = _thr_rtld_lock_destroy; li.rlock_acquire = _thr_rtld_rlock_acquire; li.wlock_acquire = _thr_rtld_wlock_acquire; li.lock_release = _thr_rtld_lock_release; li.thread_set_flag = _thr_rtld_set_flag; li.thread_clr_flag = _thr_rtld_clr_flag; li.at_fork = NULL; li.dlerror_loc = _thr_dlerror_loc; li.dlerror_loc_sz = sizeof(curthread->dlerror_msg); li.dlerror_seen = _thr_dlerror_seen; /* * Preresolve the symbols needed for the fork interposer. We * call _rtld_atfork_pre() and _rtld_atfork_post() with NULL * argument to indicate that no actual locking inside the * functions should happen. Neither rtld compat locks nor * libthr rtld locks cannot work there: * - compat locks do not handle the case of two locks taken * in write mode (the signal mask for the thread is corrupted); * - libthr locks would work, but locked rtld_bind_lock prevents * symbol resolution for _rtld_atfork_post. */ _rtld_atfork_pre(NULL); _rtld_atfork_post(NULL); _malloc_prefork(); _malloc_postfork(); getpid(); syscall(SYS_getpid); /* mask signals, also force to resolve __sys_sigprocmask PLT */ _thr_signal_block(curthread); _rtld_thread_init(&li); _thr_signal_unblock(curthread); _thr_signal_block_check_fast(); _thr_signal_block_setup(curthread); /* resolve machine depended functions, if any */ _thr_resolve_machdep(); uc_len = __getcontextx_size(); uc = alloca(uc_len); getcontext(uc); __fillcontextx2((char *)uc); }