From 40d6ef09a10c01a9338c9fc3fc10ca1b319e07af Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Tue, 28 May 2019 18:41:17 +0100 Subject: [PATCH] More fixes for RTLD depedency reduction --- lib/libc/sys/fcntl.c | 6 +- libexec/rtld-elf/Makefile | 1 - libexec/rtld-elf/libmap.c | 2 +- libexec/rtld-elf/rtld-libc/Makefile.inc | 14 ++-- libexec/rtld-elf/rtld-libc/libc_private.h | 32 +-------- libexec/rtld-elf/rtld-libc/namespace.h | 1 - .../rtld-elf/rtld-libc/rtld_avoid_libc_deps.h | 70 +++++++++++++++++++ libexec/rtld-elf/rtld-libc/rtld_libc_deps.c | 2 +- libexec/rtld-elf/rtld-libc/un-namespace.h | 2 + libexec/rtld-elf/rtld.c | 4 +- libexec/rtld-elf/rtld_lock.c | 2 +- libexec/rtld-elf/rtld_printf.c | 2 +- libexec/rtld-elf/xmalloc.c | 1 + 13 files changed, 93 insertions(+), 46 deletions(-) create mode 100644 libexec/rtld-elf/rtld-libc/rtld_avoid_libc_deps.h diff --git a/lib/libc/sys/fcntl.c b/lib/libc/sys/fcntl.c index 3c2699796910..b26f5f2096d8 100644 --- a/lib/libc/sys/fcntl.c +++ b/lib/libc/sys/fcntl.c @@ -56,13 +56,13 @@ __FBSDID("$FreeBSD$"); #ifndef __CHERI_PURE_CAPABILITY__ #pragma weak fcntl int -fcntl(int fd, int cmd, ...) +(fcntl)(int fd, int cmd, ...) #else -int _fcntl(int fd, int cmd, ...); +int (_fcntl)(int fd, int cmd, ...); __weak_reference(_fcntl, fcntl); #pragma weak _fcntl int -_fcntl(int fd, int cmd, ...) +(_fcntl)(int fd, int cmd, ...) #endif { va_list args; diff --git a/libexec/rtld-elf/Makefile b/libexec/rtld-elf/Makefile index 03c46352b5ef..53e44fd242c5 100644 --- a/libexec/rtld-elf/Makefile +++ b/libexec/rtld-elf/Makefile @@ -111,7 +111,6 @@ LDFLAGS+= -Wl,--version-script=${VERSION_MAP} SYMBOL_MAPS+= ${RTLD_ELF_DIR}/${RTLD_ARCH}/Symbol.map .endif .endif - .sinclude "${RTLD_ELF_DIR}/${RTLD_ARCH}/Makefile.inc" # Since moving rtld-elf to /libexec, we need to create a symlink. diff --git a/libexec/rtld-elf/libmap.c b/libexec/rtld-elf/libmap.c index f81ca5860a61..0a1a45a01bc1 100644 --- a/libexec/rtld-elf/libmap.c +++ b/libexec/rtld-elf/libmap.c @@ -18,7 +18,7 @@ #include "libmap.h" #include "paths.h" -#include "libc_private.h" +#include "rtld_avoid_libc_deps.h" TAILQ_HEAD(lm_list, lm); struct lm { diff --git a/libexec/rtld-elf/rtld-libc/Makefile.inc b/libexec/rtld-elf/rtld-libc/Makefile.inc index ba4a462562a8..6bc32cfe1647 100644 --- a/libexec/rtld-elf/rtld-libc/Makefile.inc +++ b/libexec/rtld-elf/rtld-libc/Makefile.inc @@ -17,9 +17,9 @@ SRCS+= opendir.c closedir.c readdir.c telldir.c SRCS+= rtld_libc_deps.c # Add this flag once everyone has a new-enough toolchain -.if ${COMPILER_VERSION} > 100000 +.if 1 || ${COMPILER_VERSION} > 100000 LDFLAGS+= -Wl,--building-freebsd-rtld -# LDFLAGS+= -Wl,--trace +LDFLAGS+= -Wl,--trace # TODO: avoid and use __sys_openat # LDFLAGS+= -Wl,--trace-symbol=open # LDFLAGS+= -Wl,--warn-if-file-linked=open.* @@ -36,10 +36,14 @@ LDFLAGS+= -Wl,--warn-if-file-linked=elf_utils.pico LDFLAGS+= -Wl,--warn-if-file-linked=_spinlock_stub.pico LDFLAGS+= -Wl,--trace-symbol=__libc_interposing -Wl,--warn-if-file-linked=interposing_table.pico LDFLAGS+= -Wl,--trace-symbol=sigprocmask -Wl,--warn-if-file-linked=sigprocmask.pico -LDFLAGS+= -Wl,--trace-symbol=read -Wl,--warn-if-file-linked=read.pico -LDFLAGS+= -Wl,--trace-symbol=write -Wl,--warn-if-file-linked=write.pico -LDFLAGS+= -Wl,--trace-symbol=close -Wl,--warn-if-file-linked=close.pico +LDFLAGS+= -Wl,--trace-symbol=_fcntl -Wl,--trace-symbol=fcntl -Wl,--warn-if-file-linked=fcntl.pico +LDFLAGS+= -Wl,--trace-symbol=_read -Wl,--trace-symbol=read -Wl,--warn-if-file-linked=read.pico +LDFLAGS+= -Wl,--trace-symbol=_write -Wl,--trace-symbol=write -Wl,--warn-if-file-linked=write.pico +LDFLAGS+= -Wl,--trace-symbol=_close -Wl,--trace-symbol=close -Wl,--warn-if-file-linked=close.pico +LDFLAGS+= -Wl,--trace-symbol=_exit -Wl,--trace-symbol=exit -Wl,--warn-if-file-linked=exit.pico LDFLAGS+= -Wl,--trace-symbol=strerror -Wl,--warn-if-file-linked=strerror.pico +LDFLAGS+= -Wl,--trace-symbol=__cleanup -Wl,--trace-symbol=__stderrp +LDFLAGS+= -Wl,--trace-symbol=_cleanup -Wl,--warn-if-file-linked=findfp.pico .endif # TODO: avoid this dependency diff --git a/libexec/rtld-elf/rtld-libc/libc_private.h b/libexec/rtld-elf/rtld-libc/libc_private.h index f720aadf5fa6..c3c4824ed4e8 100644 --- a/libexec/rtld-elf/rtld-libc/libc_private.h +++ b/libexec/rtld-elf/rtld-libc/libc_private.h @@ -1,7 +1,7 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * - * Copyright 2018 Alex Richadson + * Copyright 2019 Alex Richadson * All rights reserved. * * This software was developed by SRI International and the University of @@ -33,34 +33,6 @@ #ifndef _RTLD_LIBC_PRIVATE_H_ #define _RTLD_LIBC_PRIVATE_H_ -#include -#include -#include - -#define __isthreaded syntax error - -int __sys_fcntl(int, int, __intptr_t); -int __sys_close(int); -int __sys_openat(int, const char *, int, int); - -int __sys_sigprocmask(int, const sigset_t *, sigset_t *); -__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); - -/* - * Don't pull in any of the libc wrappers, we want to use the system call - * directly inside RTLD. - */ -#define close(fd) __sys_close(fd) -#define read(fd, buf, nbytes) __sys_read(fd, buf, nbytes) -#define write(fd, buf, nbytes) __sys_write(fd, buf, nbytes) -#define pread(fd, buf, nbytes, offset) __sys_pread(fd, buf, nbytes, offset) -#define sigprocmask(how, set, oset) __sys_sigprocmask(how, set, oset) -#define strerror(errno) rtld_strerror(errno) -/* - * TODO: use a macro for open with mandatory mode - * #define open(path, flags, mode) __sys_openat(AT_FDCWD, path, flags, mode) - */ +#include "rtld_avoid_libc_deps.h" #endif /* _RTLD_LIBC_PRIVATE_H_ */ diff --git a/libexec/rtld-elf/rtld-libc/namespace.h b/libexec/rtld-elf/rtld-libc/namespace.h index fd086a1a5fc7..5000f699fe07 100644 --- a/libexec/rtld-elf/rtld-libc/namespace.h +++ b/libexec/rtld-elf/rtld-libc/namespace.h @@ -1,4 +1,3 @@ - #define fcntl _fcntl #define open _open #define openat _openat diff --git a/libexec/rtld-elf/rtld-libc/rtld_avoid_libc_deps.h b/libexec/rtld-elf/rtld-libc/rtld_avoid_libc_deps.h new file mode 100644 index 000000000000..063faca7304c --- /dev/null +++ b/libexec/rtld-elf/rtld-libc/rtld_avoid_libc_deps.h @@ -0,0 +1,70 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright 2019 Alex Richadson + * All rights reserved. + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237 + * ("CTSRD"), as part of the DARPA CRASH 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 +#include +#include + +#define __isthreaded syntax error + +void __sys_exit(int) __dead2; +int __sys_fcntl(int, int, __intptr_t); +int __sys_close(int); +int __sys_openat(int, const char *, int, int); + +int __sys_sigprocmask(int, const sigset_t *, sigset_t *); +__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); + +/* + * Don't pull in any of the libc wrappers, we want to use the system call + * directly inside RTLD. + */ +#define _exit(status) __sys_exit(status) +#define close(fd) __sys_close(fd) +#define _close(fd) __sys_close(fd) +#define fcntl(fd, cmd, arg) __sys_fcntl(fd, cmd, arg) +#define _fcntl(fd, cmd, arg) __sys_fcntl(fd, cmd, arg) +#define read(fd, buf, nbytes) __sys_read(fd, buf, nbytes) +#define write(fd, buf, nbytes) __sys_write(fd, buf, nbytes) +#define pread(fd, buf, nbytes, offset) __sys_pread(fd, buf, nbytes, offset) +#define sigprocmask(how, set, oset) __sys_sigprocmask(how, set, oset) +#define strerror(errno) rtld_strerror(errno) +/* + * TODO: use a macro for open with mandatory mode + * #define open(path, flags, mode) __sys_openat(AT_FDCWD, path, flags, mode) + */ + +#endif /* _RTLD_AVOID_LIBC_DEPS_H_ */ diff --git a/libexec/rtld-elf/rtld-libc/rtld_libc_deps.c b/libexec/rtld-elf/rtld-libc/rtld_libc_deps.c index 2587f0d59667..ebe09acf2c81 100644 --- a/libexec/rtld-elf/rtld-libc/rtld_libc_deps.c +++ b/libexec/rtld-elf/rtld-libc/rtld_libc_deps.c @@ -9,7 +9,7 @@ #include "rtld.h" #include "rtld_printf.h" -#include "libc_private.h" +#include "rtld_avoid_libc_deps.h" /* * Avoid dependencies on various libc calls from abort(). Since this is only diff --git a/libexec/rtld-elf/rtld-libc/un-namespace.h b/libexec/rtld-elf/rtld-libc/un-namespace.h index f14e4d160754..c1882a71083b 100644 --- a/libexec/rtld-elf/rtld-libc/un-namespace.h +++ b/libexec/rtld-elf/rtld-libc/un-namespace.h @@ -5,3 +5,5 @@ #undef close #undef fstatfs #undef getdirentries + +#include "rtld_avoid_libc_deps.h" diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index 08b8a1fdf9d0..b7ea958206e2 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -75,7 +75,7 @@ __FBSDID("$FreeBSD$"); #include "rtld_malloc.h" #include "rtld_utrace.h" #include "notes.h" -#include "libc_private.h" +#include "rtld_avoid_libc_deps.h" /* Types. */ typedef void (*func_ptr_type)(void); @@ -1107,7 +1107,7 @@ rtld_die(void) rtld_fdputstr(STDERR_FILENO, _BASENAME_RTLD ": "); rtld_fdputstr(STDERR_FILENO, msg); rtld_fdputchar(STDERR_FILENO, '\n'); - _exit(1); + __sys_exit(1); } /* diff --git a/libexec/rtld-elf/rtld_lock.c b/libexec/rtld-elf/rtld_lock.c index 58c733a781ba..f10b53b83890 100644 --- a/libexec/rtld-elf/rtld_lock.c +++ b/libexec/rtld-elf/rtld_lock.c @@ -64,7 +64,7 @@ #include "debug.h" #include "rtld.h" #include "rtld_machdep.h" -#include "libc_private.h" +#include "rtld_avoid_libc_deps.h" void _rtld_thread_init(struct RtldLockInfo *) __exported; void _rtld_atfork_pre(int *) __exported; diff --git a/libexec/rtld-elf/rtld_printf.c b/libexec/rtld-elf/rtld_printf.c index 059c135dda36..b2bcb624c478 100644 --- a/libexec/rtld-elf/rtld_printf.c +++ b/libexec/rtld-elf/rtld_printf.c @@ -44,7 +44,7 @@ #include #include #include "rtld_printf.h" -#include "libc_private.h" +#include "rtld_avoid_libc_deps.h" #define MAXNBUF (sizeof(intmax_t) * NBBY + 1) diff --git a/libexec/rtld-elf/xmalloc.c b/libexec/rtld-elf/xmalloc.c index 8bad936d0101..93d50eda665a 100644 --- a/libexec/rtld-elf/xmalloc.c +++ b/libexec/rtld-elf/xmalloc.c @@ -46,6 +46,7 @@ #include "rtld.h" #include "rtld_printf.h" #include "rtld_malloc.h" +#include "rtld_avoid_libc_deps.h" void * xcalloc(size_t number, size_t size)