Skip to content

Commit

Permalink
More fixes for RTLD depedency reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
arichardson committed May 28, 2019
1 parent 2524566 commit 40d6ef0
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 46 deletions.
6 changes: 3 additions & 3 deletions lib/libc/sys/fcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion libexec/rtld-elf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion libexec/rtld-elf/libmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 9 additions & 5 deletions libexec/rtld-elf/rtld-libc/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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
Expand Down
32 changes: 2 additions & 30 deletions libexec/rtld-elf/rtld-libc/libc_private.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright 2018 Alex Richadson <[email protected]>
* Copyright 2019 Alex Richadson <[email protected]>
* All rights reserved.
*
* This software was developed by SRI International and the University of
Expand Down Expand Up @@ -33,34 +33,6 @@
#ifndef _RTLD_LIBC_PRIVATE_H_
#define _RTLD_LIBC_PRIVATE_H_

#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/fcntl.h>

#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_ */
1 change: 0 additions & 1 deletion libexec/rtld-elf/rtld-libc/namespace.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#define fcntl _fcntl
#define open _open
#define openat _openat
Expand Down
70 changes: 70 additions & 0 deletions libexec/rtld-elf/rtld-libc/rtld_avoid_libc_deps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright 2019 Alex Richadson <[email protected]>
* 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 <sys/cdefs.h>
#include <sys/types.h>
#include <sys/fcntl.h>

#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_ */
2 changes: 1 addition & 1 deletion libexec/rtld-elf/rtld-libc/rtld_libc_deps.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libexec/rtld-elf/rtld-libc/un-namespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
#undef close
#undef fstatfs
#undef getdirentries

#include "rtld_avoid_libc_deps.h"
4 changes: 2 additions & 2 deletions libexec/rtld-elf/rtld.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion libexec/rtld-elf/rtld_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libexec/rtld-elf/rtld_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#include <string.h>
#include <unistd.h>
#include "rtld_printf.h"
#include "libc_private.h"
#include "rtld_avoid_libc_deps.h"

#define MAXNBUF (sizeof(intmax_t) * NBBY + 1)

Expand Down
1 change: 1 addition & 0 deletions libexec/rtld-elf/xmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 40d6ef0

Please sign in to comment.