diff options
Diffstat (limited to 'libexec/rtld-elf/rtld-libc')
-rw-r--r-- | libexec/rtld-elf/rtld-libc/Makefile.inc | 110 | ||||
-rw-r--r-- | libexec/rtld-elf/rtld-libc/libc_private.h | 37 | ||||
-rw-r--r-- | libexec/rtld-elf/rtld-libc/namespace.h | 37 | ||||
-rw-r--r-- | libexec/rtld-elf/rtld-libc/rtld_libc.c | 120 | ||||
-rw-r--r-- | libexec/rtld-elf/rtld-libc/rtld_libc.h | 86 | ||||
-rw-r--r-- | libexec/rtld-elf/rtld-libc/un-namespace.h | 39 |
6 files changed, 429 insertions, 0 deletions
diff --git a/libexec/rtld-elf/rtld-libc/Makefile.inc b/libexec/rtld-elf/rtld-libc/Makefile.inc new file mode 100644 index 000000000000..a10bd562a7ce --- /dev/null +++ b/libexec/rtld-elf/rtld-libc/Makefile.inc @@ -0,0 +1,110 @@ +# This makefiles adds the necessary libc dependencies for RTLD without pulling +# in all of the complex libc bits such as locales, etc. + +.include <bsd.compiler.mk> + +LIBC_SRCTOP=${SRCTOP}/lib/libc +.if exists(${LIBC_SRCTOP}/${MACHINE_ARCH:S/powerpc64le/powerpc64/}) +LIBC_ARCH=${MACHINE_ARCH:S/powerpc64le/powerpc64/} +.else +LIBC_ARCH=${MACHINE_CPUARCH} +.endif + +CFLAGS+= -I${SRCTOP}/libexec/rtld-elf/rtld-libc + +# Build all the libc files that use interposed symbols or pthreads again for +# RTLD. We compile with a different libc_private.h and namespace.h that +# redirects all calls to interposed functions to use the non-interposed version +# instead. +.PATH: ${LIBC_SRCTOP}/gen +SRCS+= fdopendir.c opendir.c opendir2.c closedir.c readdir.c telldir.c + +# Avoid further dependencies by providing simple implementations of libc +# functions such as __error(), etc. +.PATH: ${SRCTOP}/libexec/rtld-elf/rtld-libc +SRCS+= rtld_libc.c + +# Now build the remaining files from libc: +.PATH: ${LIBC_SRCTOP}/stdlib +SRCS+= reallocf.c realpath.c merge.c reallocarray.c +# TODO: fix merge.c to build with WARNS=6 +.if ${COMPILER_TYPE} == "clang" +CFLAGS.merge.c+=-Wno-error=null-pointer-arithmetic +.endif +.PATH: ${LIBC_SRCTOP}/gen +SRCS+= errlst.c getcwd.c getprogname.c raise.c sigsetops.c sysctlnametomib.c \ + __xuname.c +# errlst.c needs the errlst.h header from libc: +CFLAGS.errlst.c+=-I${LIBC_SRCTOP}/include + +# use generic versions of string functions to avoid potential ifunc dispatch +.PATH: ${LIBC_SRCTOP}/string +SRCS+= bcopy.c bzero.c memchr.c memcmp.c memcpy.c memmove.c memset.c strcat.c \ + strchr.c strchrnul.c strcmp.c strcpy.c strcspn.c strdup.c strlcat.c \ + strlcpy.c strlen.c strncmp.c strncpy.c strrchr.c strsep.c strspn.c \ + strstr.c strtok.c +CFLAGS.memchr.c+=-Wno-cast-qual +CFLAGS.strchr.c+=-Wno-cast-qual +CFLAGS.strchrnul.c+=-Wno-cast-qual +CFLAGS.strcspn.c+=-Wno-sign-compare +CFLAGS.strrchr.c+=-Wno-cast-qual +CFLAGS.strspn.c+=-Wno-sign-compare +CFLAGS.strstr.c+=-Wno-cast-qual -Wno-sign-compare +CFLAGS.strtok.c+=-Wno-cast-qual + +# Also use all the syscall .o files from libsys_pic (libsys is always NO_SSP): +_libsys_other_objects= fstat fstatat fstatfs syscall \ + cerror geteuid getegid sigfastblock munmap mprotect \ + sysarch __sysctl issetugid __getcwd utrace getpid \ + thr_self thr_kill pread mmap lseek _exit \ + getdirentries _close _fcntl _open _openat _read \ + _sigprocmask _write readlink ___realpathat +# A few other bits from libc_nossp_pic: +_libc_other_objects= sigsetjmp lstat stat _setjmp setjmp setjmperr + +# Finally add additional architecture-dependent libc and libsys dependencies +.if ${LIBC_ARCH} == "arm" +# ARM needs aeabi_unwind_cpp for _setjmp +_libc_other_objects+=aeabi_unwind_cpp +.elif ${LIBC_ARCH} == "i386" +# i386 needs i386_set_gsbase for allocate_initial_tls() +_libsys_other_objects+=i386_set_gsbase +.elif ${LIBC_ARCH} == "powerpc" || ${LIBC_ARCH} == "powerpcspe" +# ppc needs __syncicache and abs for reloc.c +_libc_other_objects+=syncicache abs +.elif ${LIBC_ARCH} == "powerpc64" +# ppc64 needs __syncicache for reloc.c +_libc_other_objects+=syncicache +.endif + +# Extract all the .o files from libc_nossp_pic.a and libsys_pic.a. This +# ensures that we don't accidentally pull in the interposing table or +# similar by linking directly against libc_nossp_pic.a +_rtld_libc_objs= +.for _obj in ${_libc_other_objects} +_rtld_libc_objs+=${_obj}.nossppico +CLEANFILES+=${_obj}.nossppico +# LDFLAGS+= -Wl,--trace-symbol=${_obj} +.endfor +_rtld_libsys_objs= +.for _obj in ${_libsys_other_objects} +_rtld_libsys_objs+=${_obj}.pico +CLEANFILES+=${_obj}.pico +# LDFLAGS+= -Wl,--trace-symbol=${_obj} +.endfor +# LDFLAGS+= -Wl,--trace + +# We insert all the .o files from libc_nossp_pic.a into a new rtld_libc.a file +# to ensure that only .o files that are actually used end up being included. +# +# XXX: we add dependencies on the source libraries in ../Makefile after +# bsd.prog.mk includes bsd.libnames.mk since variables in dependencies are +# expanded when parsed. +rtld_libc.a: ${SRCTOP}/libexec/rtld-elf/rtld-libc/Makefile.inc + @rm -f ${.TARGET} + ${AR} x ${LIBC_NOSSP_PIC} ${_rtld_libc_objs} + ${AR} x ${LIBSYS_PIC} ${_rtld_libsys_objs} + ${AR} cr ${.TARGET} ${_rtld_libc_objs} ${_rtld_libsys_objs} +CLEANFILES+=rtld_libc.a +LDADD+=${.OBJDIR}/rtld_libc.a +beforelinking: rtld_libc.a diff --git a/libexec/rtld-elf/rtld-libc/libc_private.h b/libexec/rtld-elf/rtld-libc/libc_private.h new file mode 100644 index 000000000000..0cfb0e562733 --- /dev/null +++ b/libexec/rtld-elf/rtld-libc/libc_private.h @@ -0,0 +1,37 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright 2019 Alex Richardson <arichardson@FreeBSD.org> + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory (Department of Computer Science and + * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the + * DARPA SSITH research programme. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. + */ +#ifndef _RTLD_LIBC_PRIVATE_H_ +#define _RTLD_LIBC_PRIVATE_H_ + +#include "rtld_libc.h" + +#endif /* _RTLD_LIBC_PRIVATE_H_ */ diff --git a/libexec/rtld-elf/rtld-libc/namespace.h b/libexec/rtld-elf/rtld-libc/namespace.h new file mode 100644 index 000000000000..571489d6ff19 --- /dev/null +++ b/libexec/rtld-elf/rtld-libc/namespace.h @@ -0,0 +1,37 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright 2019 Alex Richardson <arichardson@FreeBSD.org> + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory (Department of Computer Science and + * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the + * DARPA SSITH research programme. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. + */ +#define fcntl _fcntl +#define open _open +#define openat _openat +#define fstatfs _fstatfs +#define close _close +#define getdirentries _getdirentries diff --git a/libexec/rtld-elf/rtld-libc/rtld_libc.c b/libexec/rtld-elf/rtld-libc/rtld_libc.c new file mode 100644 index 000000000000..a5371de394e9 --- /dev/null +++ b/libexec/rtld-elf/rtld-libc/rtld_libc.c @@ -0,0 +1,120 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright 2019 Alex Richardson <arichardson@FreeBSD.org> + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory (Department of Computer Science and + * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the + * DARPA SSITH research programme. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 <sys/param.h> +#include <sys/types.h> +#include <sys/sysctl.h> +#include <assert.h> +#include <signal.h> +#include <stdarg.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> + +#include "rtld.h" +#include "rtld_printf.h" +#include "rtld_libc.h" + +/* + * Avoid dependencies from various libc calls on abort(). Since this is only + * used for assertions in RTLD, we can just raise SIGABRT directly. + */ +void +abort(void) +{ + raise(SIGABRT); + __builtin_trap(); +} + +static int rtld_errno; +int *__error(void); +int * +__error(void) +{ + + return (&rtld_errno); +} + +/* Avoid dependency on __libc_interposing, use the system call directly. */ +#undef sigprocmask +int +sigprocmask(int how, const sigset_t *set, sigset_t *oset) +{ + + return (__sys_sigprocmask(how, set, oset)); +} +__strong_reference(sigprocmask, __libc_sigprocmask); + +#if defined DEBUG || !defined(NDEBUG) +/* Provide an implementation of __assert that does not pull in fprintf() */ +void +__assert(const char *func, const char *file, int line, const char *failedexpr) +{ + + if (func == NULL) + (void)rtld_fdprintf(STDERR_FILENO, + "Assertion failed: (%s), file %s, line %d.\n", failedexpr, + file, line); + else + (void)rtld_fdprintf(STDERR_FILENO, + "Assertion failed: (%s), function %s, file %s, line %d.\n", + failedexpr, func, file, line); + abort(); + /* NOTREACHED */ +} +#endif + +/* + * Avoid pulling in all of pthreads from getpagesize(). + * It normally uses libc/gen/auxv.c which pulls in pthread_once(). + * This relies on init_pagesizes setting page_size so must not be called + * before that. + */ +int +getpagesize(void) +{ + return (page_size); +} + +int __sys___sysctl(const int *name, u_int namelen, void *oldp, + size_t *oldlenp, const void *newp, size_t newlen); + +int +sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp, + const void *newp, size_t newlen) +{ + int retval; + + assert(name[0] != CTL_USER && "Not supported inside rtld!"); + retval = __sys___sysctl(name, namelen, oldp, oldlenp, newp, newlen); + return (retval); +} diff --git a/libexec/rtld-elf/rtld-libc/rtld_libc.h b/libexec/rtld-elf/rtld-libc/rtld_libc.h new file mode 100644 index 000000000000..33601d73cbf9 --- /dev/null +++ b/libexec/rtld-elf/rtld-libc/rtld_libc.h @@ -0,0 +1,86 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright 2019 Alex Richardson <arichardson@FreeBSD.org> + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory (Department of Computer Science and + * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the + * DARPA SSITH research programme. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. + */ +#ifndef _RTLD_AVOID_LIBC_DEPS_H_ +#define _RTLD_AVOID_LIBC_DEPS_H_ + +#include <sys/types.h> +#include <sys/fcntl.h> +#include <sys/stat.h> + +/* Avoid dependencies on libthr (used by closedir/opendir/readdir) */ +#define __isthreaded 0 +#define _pthread_mutex_lock(mtx) (void)0 +#define _pthread_mutex_unlock(mtx) (void)0 +#define _pthread_mutex_destroy(mtx) (void)0 +#define __libc_interposing error, must not use this variable inside rtld + +int __sys_close(int); +void __sys__exit(int) __dead2; +int __sys_fcntl(int, int, ...); +int __sys_fstat(int fd, struct stat *); +int __sys_fstatat(int, const char *, struct stat *, int); +int __sys___getcwd(char *, size_t); +int __sys_open(const char *, int, ...); +int __sys_openat(int, const char *, int, ...); +int __sys_sigprocmask(int, const sigset_t *, sigset_t *); +int __sys_thr_kill(long, int); +int __sys_thr_self(long *); +__ssize_t __sys_pread(int, void *, __size_t, __off_t); +__ssize_t __sys_read(int, void *, __size_t); +__ssize_t __sys_write(int, const void *, __size_t); + +extern char* __progname; +const char *_getprogname(void); +int __getosreldate(void); + + +/* + * Don't pull in any of the libc wrappers. Instead we use the system call + * directly inside RTLD to avoid pulling in __libc_interposing (which pulls + * in lots more object files). + */ +#define close(fd) __sys_close(fd) +#define _close(fd) __sys_close(fd) +#define exit(status) __sys__exit(status) +#define _exit(status) __sys__exit(status) +#define fcntl(fd, cmd, arg) __sys_fcntl(fd, cmd, arg) +#define _fcntl(fd, cmd, arg) __sys_fcntl(fd, cmd, arg) +#define _fstat(fd, sb) __sys_fstat(fd, sb) +#define open(path, ...) __sys_open(path, __VA_ARGS__) +#define pread(fd, buf, nbytes, offset) __sys_pread(fd, buf, nbytes, offset) +#define read(fd, buf, nbytes) __sys_read(fd, buf, nbytes) +#define sigprocmask(how, set, oset) __sys_sigprocmask(how, set, oset) +#define strerror(errno) rtld_strerror(errno) +#define _write(fd, buf, nbytes) __sys_write(fd, buf, nbytes) +#define write(fd, buf, nbytes) __sys_write(fd, buf, nbytes) + +#endif /* _RTLD_AVOID_LIBC_DEPS_H_ */ diff --git a/libexec/rtld-elf/rtld-libc/un-namespace.h b/libexec/rtld-elf/rtld-libc/un-namespace.h new file mode 100644 index 000000000000..dcec40b4a27f --- /dev/null +++ b/libexec/rtld-elf/rtld-libc/un-namespace.h @@ -0,0 +1,39 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright 2019 Alex Richardson <arichardson@FreeBSD.org> + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory (Department of Computer Science and + * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the + * DARPA SSITH research programme. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. + */ +#undef fcntl +#undef open +#undef openat +#undef close +#undef fstatfs +#undef getdirentries + +#include "rtld_libc.h" |