aboutsummaryrefslogtreecommitdiff
path: root/tools/build
diff options
context:
space:
mode:
authorAlex Richardson <arichardson@FreeBSD.org>2020-08-25 13:23:31 +0000
committerAlex Richardson <arichardson@FreeBSD.org>2020-08-25 13:23:31 +0000
commit7ec1ec4fdb98d87602c8501dae9b9cbd24b7d22b (patch)
treee0c68e3e2ec5ef91266afe23d2e5423bc06489a1 /tools/build
parentd141d1cc5f9a204a1eeb4b564e4cfa1821fe287e (diff)
downloadsrc-7ec1ec4fdb98d87602c8501dae9b9cbd24b7d22b.tar.gz
src-7ec1ec4fdb98d87602c8501dae9b9cbd24b7d22b.zip
Add missing FreeBSD functions to -legacy when building on macOS/Linux
In most cases this simply builds the file from lib/libc for missing functions (e.g. strlcpy on Linux etc.). In cases where this is not possible I've added an implementation to tools/build/cross-build. The fgetln.c/fgetwln.c/closefrom.c compatibility code was obtained from https://gitlab.freedesktop.org/libbsd/libbsd, but I'm not sure it makes sense to import it into to contrib just for these three bootstrap files. Reviewed By: brooks Differential Revision: https://reviews.freebsd.org/D25978
Notes
Notes: svn path=/head/; revision=364759
Diffstat (limited to 'tools/build')
-rw-r--r--tools/build/Makefile190
-rw-r--r--tools/build/cross-build/capsicum_stubs.c67
-rw-r--r--tools/build/cross-build/closefrom.c192
-rw-r--r--tools/build/cross-build/fake_sysctl.c58
-rw-r--r--tools/build/cross-build/fgetln_fallback.c84
-rw-r--r--tools/build/cross-build/fgetwln_fallback.c93
-rw-r--r--tools/build/cross-build/local-link.h38
-rw-r--r--tools/build/cross-build/progname.c53
-rw-r--r--tools/build/libc-bootstrap/libc_private.h40
-rw-r--r--tools/build/libc-bootstrap/namespace.h52
-rw-r--r--tools/build/libc-bootstrap/un-namespace.h40
-rw-r--r--tools/build/mk/Makefile.boot68
-rw-r--r--tools/build/mk/Makefile.boot.pre12
-rw-r--r--tools/build/mk/bsd.lib.mk3
-rw-r--r--tools/build/mk/bsd.prog.mk1
15 files changed, 976 insertions, 15 deletions
diff --git a/tools/build/Makefile b/tools/build/Makefile
index 9a5db708bb2f..da8ff9cc4a93 100644
--- a/tools/build/Makefile
+++ b/tools/build/Makefile
@@ -5,6 +5,7 @@
LIB= egacy
SRC=
INCSGROUPS= INCS SYSINCS CASPERINC UFSINCS FFSINCS MSDOSFSINCS DISKINCS
+INCSGROUPS+= MACHINESYSINCS RPCINCS
INCS=
SYSINCSDIR= ${INCLUDEDIR}/sys
@@ -14,49 +15,83 @@ UFSINCSDIR= ${INCLUDEDIR}/ufs/ufs
FFSINCSDIR= ${INCLUDEDIR}/ufs/ffs
MSDOSFSINCSDIR= ${INCLUDEDIR}/fs/msdosfs
DISKINCSDIR= ${INCLUDEDIR}/sys/disk
+MACHINESYSINCSDIR= ${INCLUDEDIR}/machine
+RPCINCSDIR= ${INCLUDEDIR}/rpc
BOOTSTRAPPING?= 0
-_WITH_PWCACHEDB!= grep -c pwcache_groupdb /usr/include/grp.h || true
+
+.if ${.MAKE.OS} == "Darwin"
+_XCODE_ROOT!=xcode-select -p
+# since macOS 10.14 C headers are no longer installed in /usr but only
+# provided via the SDK
+.if ${_XCODE_ROOT} == "/Library/Developer/CommandLineTools"
+# Only command line tools installed -> host headers are in the SDKs directory
+_MACOS_SDK_DIR=${_XCODE_ROOT}/SDKs/MacOSX.sdk/
+.else
+# Full XCode installed -> host headers are below Platforms/MacOSX.platform
+_MACOS_SDK_DIR=${_XCODE_ROOT}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
+.endif
+HOST_INCLUDE_ROOT=${_MACOS_SDK_DIR}/usr/include
+.if !exists(${HOST_INCLUDE_ROOT}/stdio.h)
+.error "You must install the macOS SDK (try xcode-select --install)"
+.endif
+.else
+HOST_INCLUDE_ROOT=/usr/include
+.endif
+
+# Allow building libc-internal files (also on non-FreeBSD hosts)
+CFLAGS+= -I${.CURDIR}/libc-bootstrap
+# Symbol versioning is not required for -legacy (and macOS bootstrap)
+MK_SYMVER= no
+
+_WITH_PWCACHEDB!= grep -c pwcache_groupdb ${HOST_INCLUDE_ROOT}/grp.h || true
.if ${_WITH_PWCACHEDB} == 0
.PATH: ${.CURDIR}/../../contrib/libc-pwcache
-CFLAGS+= -I${.CURDIR}/../../contrib/libc-pwcache \
- -I${.CURDIR}/../../lib/libc/include
+CFLAGS.pwcache.c+= -I${.CURDIR}/../../contrib/libc-pwcache
SRCS+= pwcache.c
.endif
-_WITH_STRSVIS!= grep -c strsvis /usr/include/vis.h || true
+_WITH_STRSVIS!= grep -c strsvis ${HOST_INCLUDE_ROOT}/vis.h 2>/dev/null || true
.if ${_WITH_STRSVIS} == 0
.PATH: ${.CURDIR}/../../contrib/libc-vis
-SRCS+= vis.c
-CFLAGS+= -I${.CURDIR}/../../contrib/libc-vis \
- -I${.CURDIR}/../../lib/libc/include
+INCS+= vis.h
+SRCS+= vis.c unvis.c
+CFLAGS.vis.c+= -I${.CURDIR}/../../contrib/libc-vis
+CFLAGS.unvis.c+= -I${.CURDIR}/../../contrib/libc-vis
.endif
-_WITH_REALLOCARRAY!= grep -c reallocarray /usr/include/stdlib.h || true
+_WITH_REALLOCARRAY!= grep -c reallocarray ${HOST_INCLUDE_ROOT}/stdlib.h || true
.if ${_WITH_REALLOCARRAY} == 0
.PATH: ${.CURDIR}/../../lib/libc/stdlib
INCS+= stdlib.h
SRCS+= reallocarray.c
-CFLAGS+= -I${.CURDIR}/../../lib/libc/include
.endif
-_WITH_UTIMENS!= grep -c utimensat /usr/include/sys/stat.h || true
+_WITH_UTIMENS!= grep -c utimensat ${HOST_INCLUDE_ROOT}/sys/stat.h || true
.if ${_WITH_UTIMENS} == 0
SYSINCS+= stat.h
SRCS+= futimens.c utimensat.c
.endif
-_WITH_EXPLICIT_BZERO!= grep -c explicit_bzero /usr/include/strings.h || true
+_WITH_EXPLICIT_BZERO!= grep -c explicit_bzero ${HOST_INCLUDE_ROOT}/strings.h || true
.if ${_WITH_EXPLICIT_BZERO} == 0
-.PATH: ${SRCTOP}/sys/libkern
+# .PATH: ${SRCTOP}/sys/libkern
+# Adding sys/libkern to .PATH breaks building the cross-build compat library
+# since that attempts to build strlcpy.c from libc and adding libkern here will
+# cause it to pick the file from libkern instead (which won't compile).
+# Avoid modifying .PATH by creating a copy in the build directory instead.
+explicit_bzero.c: ${SRCTOP}/sys/libkern/explicit_bzero.c
+ cp ${.ALLSRC} ${.TARGET}
+CLEANFILES+= explicit_bzero.c
INCS+= strings.h
SRCS+= explicit_bzero.c
.endif
-.if exists(/usr/include/capsicum_helpers.h)
-_WITH_CAPH_ENTER!= grep -c caph_enter /usr/include/capsicum_helpers.h || true
-_WITH_CAPH_RIGHTS_LIMIT!= grep -c caph_rights_limit /usr/include/capsicum_helpers.h || true
+
+.if exists(${HOST_INCLUDE_ROOT}/capsicum_helpers.h)
+_WITH_CAPH_ENTER!= grep -c caph_enter ${HOST_INCLUDE_ROOT}/capsicum_helpers.h || true
+_WITH_CAPH_RIGHTS_LIMIT!= grep -c caph_rights_limit ${HOST_INCLUDE_ROOT}/capsicum_helpers.h || true
.endif
.if !defined(_WITH_CAPH_ENTER) || ${_WITH_CAPH_ENTER} == 0 || ${_WITH_CAPH_RIGHTS_LIMIT} == 0
.PATH: ${SRCTOP}/lib/libcapsicum
@@ -65,6 +100,105 @@ INCS+= capsicum_helpers.h
INCS+= libcasper.h
.endif
+# rpcgen should build against the source tree rpc/types.h and not the host.
+# This is especially important on non-FreeBSD systems where the types may
+# not match.
+RPCINCS+= ${SRCTOP}/sys/rpc/types.h
+
+.if ${.MAKE.OS} != "FreeBSD"
+.PATH: ${.CURDIR}/cross-build
+
+INCS+= ${SRCTOP}/include/mpool.h
+INCS+= ${SRCTOP}/include/ndbm.h
+INCS+= ${SRCTOP}/include/err.h
+INCS+= ${SRCTOP}/include/stringlist.h
+
+# Needed to build arc4random.c
+INCSGROUPS+= CHACHA20INCS
+CHACHA20INCSDIR= ${INCLUDEDIR}/crypto/chacha20
+CHACHA20INCS+= ${SRCTOP}/sys/crypto/chacha20/_chacha.h \
+ ${SRCTOP}/sys/crypto/chacha20/chacha.h
+
+_host_arch=${MACHINE}
+.if ${_host_arch} == "x86_64"
+# bmake on Linux/mac often prints that instead of amd64
+_host_arch=amd64
+.endif
+.if ${_host_arch} == "unknown"
+# HACK: If MACHINE is unknown, assume we are building on x86
+_host_arch=amd64
+.endif
+MACHINESYSINCS+= ${SRCTOP}/sys/${_host_arch}/include/elf.h
+.if ${_host_arch} == "amd64" || ${_host_arch} == "i386"
+INCSGROUPS+= X86INCS
+X86INCSDIR= ${INCLUDEDIR}/x86
+X86INCS+= ${SRCTOP}/sys/x86/include/elf.h
+.endif
+
+# needed for btxld:
+MACHINESYSINCS+= ${SRCTOP}/sys/${_host_arch}/include/exec.h
+MACHINESYSINCS+= ${SRCTOP}/sys/${_host_arch}/include/reloc.h
+INCS+= ${SRCTOP}/include/a.out.h
+INCS+= ${SRCTOP}/include/nlist.h
+SYSINCS+= ${SRCTOP}/sys/sys/imgact_aout.h
+SYSINCS+= ${SRCTOP}/sys/sys/nlist_aout.h
+
+# dbopen() behaves differently on Linux and FreeBSD so we ensure that we
+# bootstrap the FreeBSD db code. The cross-build headers #define dbopen() to
+# __freebsd_dbopen() so that we don't ever use the host version
+INCS+= ${SRCTOP}/include/db.h
+LIBC_SRCTOP= ${SRCTOP}/lib/libc/
+.include "${LIBC_SRCTOP}/db/Makefile.inc"
+# Do the same as we did for dbopen() for getopt() on since it's not compatible
+# on Linux (and to avoid surprises also compile the FreeBSD code on macOS)
+.PATH: ${LIBC_SRCTOP}/stdlib
+SRCS+= getopt.c getopt_long.c
+INCS+= ${SRCTOP}/include/getopt.h
+
+# getcap.c is needed for cap_mkdb:
+.PATH: ${LIBC_SRCTOP}/gen
+SRCS+= getcap.c
+# Add various libbc functions that are not available in glibc:
+SRCS+= stringlist.c setmode.c
+SRCS+= strtonum.c merge.c heapsort.c reallocf.c
+.PATH: ${LIBC_SRCTOP}/locale
+SRCS+= rpmatch.c
+
+.if ${.MAKE.OS} == "Linux"
+# On Linux, glibc does not provide strlcpy,strlcat or strmode.
+.PATH: ${LIBC_SRCTOP}/string
+SRCS+= strlcpy.c strlcat.c strmode.c
+# Compile the fgetln/fgetwln/closefrom fallback code from libbsd:
+SRCS+= fgetln_fallback.c fgetwln_fallback.c closefrom.c
+CFLAGS.closefrom.c+= -DSTDC_HEADERS -DHAVE_SYS_DIR_H -DHAVE_DIRENT_H \
+ -DHAVE_DIRFD -DHAVE_SYSCONF
+# Provide warnc/errc/getprogname/setprograme
+SRCS+= err.c progname.c
+.endif
+# Provide the same arc4random implementation on Linux/macOS
+CFLAGS.arc4random.c+= -I${SRCTOP}/sys/crypto/chacha20 -D__isthreaded=1
+SRCS+= arc4random.c arc4random_uniform.c
+
+# expand_number() is not provided by either Linux or MacOS libutil
+.PATH: ${SRCTOP}/lib/libutil
+SRCS+= expand_number.c
+# Linux libutil also doesn't have fparseln
+SRCS+= fparseln.c
+# A dummy sysctl for tzsetup:
+SRCS+= fake_sysctl.c
+
+# capsicum support
+SYSINCS+= ${SRCTOP}/sys/sys/capsicum.h
+SYSINCS+= ${SRCTOP}/sys/sys/caprights.h
+SRCS+= capsicum_stubs.c
+# XXX: we can't add ${SRCTOP}/sys/kern to .PATH since that will causes
+# conflicts with other files. Instead copy subr_capability to the build dir.
+subr_capability.c: ${SRCTOP}/sys/kern/subr_capability.c
+ cp ${.ALLSRC} ${.TARGET}
+SRCS+= subr_capability.c
+CLEANFILES+= subr_capability.c
+.endif
+
CASPERINC+= ${SRCTOP}/lib/libcasper/services/cap_fileargs/cap_fileargs.h
.if empty(SRCS)
@@ -97,9 +231,22 @@ SYSINCS+= ${SRCTOP}/sys/sys/elf32.h
SYSINCS+= ${SRCTOP}/sys/sys/elf64.h
SYSINCS+= ${SRCTOP}/sys/sys/elf_common.h
SYSINCS+= ${SRCTOP}/sys/sys/elf_generic.h
+SYSINCS+= ${SRCTOP}/sys/sys/queue.h
+SYSINCS+= ${SRCTOP}/sys/sys/md5.h
+SYSINCS+= ${SRCTOP}/sys/sys/sbuf.h
+SYSINCS+= ${SRCTOP}/sys/sys/tree.h
# vtfontcvt is using sys/font.h
SYSINCS+= ${SRCTOP}/sys/sys/font.h
+# For mkscrfil.c:
+SYSINCS+= ${SRCTOP}/sys/sys/consio.h
+# for gencat:
+INCS+= ${SRCTOP}/include/nl_types.h
+# for vtfontcvt:
+SYSINCS+= ${SRCTOP}/sys/sys/fnv_hash.h
+# opensolaris compatibility
+INCS+= ${SRCTOP}/include/elf.h
+SYSINCS+= ${SRCTOP}/sys/sys/elf.h
# We want to run the build with only ${WORLDTMP} in $PATH to ensure we don't
# accidentally run tools that are incompatible but happen to be in $PATH.
@@ -122,6 +269,19 @@ _host_tools_to_symlink= basename bzip2 bunzip2 chmod chown cmp comm cp date dd \
_make_abs!= which "${MAKE}"
_host_abs_tools_to_symlink= ${_make_abs}:make ${_make_abs}:bmake
+.if ${.MAKE.OS} != "FreeBSD"
+_make_abs!= which "${MAKE}"
+_host_abs_tools_to_symlink+= ${_make_abs}:make ${_make_abs}:bmake
+.if ${.MAKE.OS} == "Darwin"
+# /usr/bin/cpp may invoke xcrun:
+_host_tools_to_symlink+=xcrun
+.endif # ${.MAKE.OS} == "Darwin"
+# On Ubuntu /bin/sh is dash which is totally useless. Let's just link bash
+# as the build sh since that will work fine.
+_host_abs_tools_to_symlink+= /bin/bash:sh
+_host_tools_to_symlink:=${_host_tools_to_symlink:Nsh}
+.endif
+
host-symlinks:
@echo "Linking host tools into ${DESTDIR}/bin"
.for _tool in ${_host_tools_to_symlink}
diff --git a/tools/build/cross-build/capsicum_stubs.c b/tools/build/cross-build/capsicum_stubs.c
new file mode 100644
index 000000000000..b042796c069f
--- /dev/null
+++ b/tools/build/cross-build/capsicum_stubs.c
@@ -0,0 +1,67 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2018-2020 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.
+ *
+ * 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.
+ */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/capsicum.h>
+
+#include <errno.h>
+
+int
+cap_ioctls_limit(int fd, const cap_ioctl_t *cmds, size_t ncmds)
+{
+ return 0; /* Just pretend that it succeeded */
+}
+
+int
+cap_fcntls_limit(int fd, uint32_t fcntlrights)
+{
+ return 0; /* Just pretend that it succeeded */
+}
+
+int
+cap_rights_limit(int fd, const cap_rights_t *rights)
+{
+ return 0; /* Just pretend that it succeeded */
+}
+
+int
+cap_enter(void)
+{
+ errno = ENOSYS;
+ return -1;
+}
diff --git a/tools/build/cross-build/closefrom.c b/tools/build/cross-build/closefrom.c
new file mode 100644
index 000000000000..9d037f0efe89
--- /dev/null
+++ b/tools/build/cross-build/closefrom.c
@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2004-2005, 2007, 2010, 2012-2014
+ * Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+// #include <config.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdio.h>
+#ifdef STDC_HEADERS
+# include <stdlib.h>
+# include <stddef.h>
+#else
+# ifdef HAVE_STDLIB_H
+# include <stdlib.h>
+# endif
+#endif /* STDC_HEADERS */
+#include <fcntl.h>
+#include <limits.h>
+#ifdef HAVE_PSTAT_GETPROC
+# include <sys/param.h>
+# include <sys/pstat.h>
+#else
+# ifdef HAVE_DIRENT_H
+# include <dirent.h>
+# define NAMLEN(dirent) strlen((dirent)->d_name)
+# else
+# define dirent direct
+# define NAMLEN(dirent) (dirent)->d_namlen
+# ifdef HAVE_SYS_NDIR_H
+# include <sys/ndir.h>
+# endif
+# ifdef HAVE_SYS_DIR_H
+# include <sys/dir.h>
+# endif
+# ifdef HAVE_NDIR_H
+# include <ndir.h>
+# endif
+# endif
+#endif
+
+#ifndef OPEN_MAX
+# define OPEN_MAX 256
+#endif
+
+#if defined(HAVE_FCNTL_CLOSEM) && !defined(HAVE_DIRFD)
+# define closefrom closefrom_fallback
+#endif
+
+static inline void
+closefrom_close(int fd)
+{
+#ifdef __APPLE__
+ /* Avoid potential libdispatch crash when we close its fds. */
+ (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
+#else
+ (void)close(fd);
+#endif
+}
+
+/*
+ * Close all file descriptors greater than or equal to lowfd.
+ * This is the expensive (fallback) method.
+ */
+void
+closefrom_fallback(int lowfd)
+{
+ long fd, maxfd;
+
+ /*
+ * Fall back on sysconf() or getdtablesize(). We avoid checking
+ * resource limits since it is possible to open a file descriptor
+ * and then drop the rlimit such that it is below the open fd.
+ */
+#ifdef HAVE_SYSCONF
+ maxfd = sysconf(_SC_OPEN_MAX);
+#else
+ maxfd = getdtablesize();
+#endif /* HAVE_SYSCONF */
+ if (maxfd < 0)
+ maxfd = OPEN_MAX;
+
+ for (fd = lowfd; fd < maxfd; fd++)
+ closefrom_close(fd);
+}
+
+/*
+ * Close all file descriptors greater than or equal to lowfd.
+ * We try the fast way first, falling back on the slow method.
+ */
+#if defined(HAVE_FCNTL_CLOSEM)
+void
+closefrom(int lowfd)
+{
+ if (fcntl(lowfd, F_CLOSEM, 0) == -1)
+ closefrom_fallback(lowfd);
+}
+#elif defined(HAVE_PSTAT_GETPROC)
+void
+closefrom(int lowfd)
+{
+ struct pst_status pstat;
+ int fd;
+
+ if (pstat_getproc(&pstat, sizeof(pstat), 0, getpid()) != -1) {
+ for (fd = lowfd; fd <= pstat.pst_highestfd; fd++)
+ (void)close(fd);
+ } else {
+ closefrom_fallback(lowfd);
+ }
+}
+#elif defined(HAVE_DIRFD)
+static int
+closefrom_procfs(int lowfd)
+{
+ const char *path;
+ DIR *dirp;
+ struct dirent *dent;
+ int *fd_array = NULL;
+ int fd_array_used = 0;
+ int fd_array_size = 0;
+ int ret = 0;
+ int i;
+
+ /* Use /proc/self/fd (or /dev/fd on FreeBSD) if it exists. */
+# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
+ path = "/dev/fd";
+# else
+ path = "/proc/self/fd";
+# endif
+ dirp = opendir(path);
+ if (dirp == NULL)
+ return -1;
+
+ while ((dent = readdir(dirp)) != NULL) {
+ const char *errstr;
+ int fd;
+
+ fd = strtonum(dent->d_name, lowfd, INT_MAX, &errstr);
+ if (errstr != NULL || fd == dirfd(dirp))
+ continue;
+
+ if (fd_array_used >= fd_array_size) {
+ int *ptr;
+
+ if (fd_array_size > 0)
+ fd_array_size *= 2;
+ else
+ fd_array_size = 32;
+
+ ptr = reallocarray(fd_array, fd_array_size, sizeof(int));
+ if (ptr == NULL) {
+ ret = -1;
+ break;
+ }
+ fd_array = ptr;
+ }
+
+ fd_array[fd_array_used++] = fd;
+ }
+
+ for (i = 0; i < fd_array_used; i++)
+ closefrom_close(fd_array[i]);
+
+ free(fd_array);
+ (void)closedir(dirp);
+
+ return ret;
+}
+
+void
+closefrom(int lowfd)
+{
+ if (closefrom_procfs(lowfd) == 0)
+ return;
+
+ closefrom_fallback(lowfd);
+}
+#endif /* HAVE_FCNTL_CLOSEM */
diff --git a/tools/build/cross-build/fake_sysctl.c b/tools/build/cross-build/fake_sysctl.c
new file mode 100644
index 000000000000..7b9524e1626b
--- /dev/null
+++ b/tools/build/cross-build/fake_sysctl.c
@@ -0,0 +1,58 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2018-2020 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.
+ *
+ * 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.
+ */
+/* This file contains wrappers for sysctls used during build/install */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <err.h>
+#include <string.h>
+#include <sysexits.h>
+
+int
+__freebsd_sysctlbyname(
+ const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
+{
+ if (strcmp(name, "kern.vm_guest") == 0) {
+ if (!oldp || !oldlenp)
+ errx(EX_USAGE, "Missing arguments for kern.vm_guest");
+
+ if (newp || newlen)
+ errx(EX_USAGE, "kern.vm_guest is read-only");
+ strlcpy(oldp, "none", *oldlenp);
+ *oldlenp = strlen("none");
+ }
+ errx(EX_USAGE, "fatal: unknown sysctl %s\n", name);
+}
diff --git a/tools/build/cross-build/fgetln_fallback.c b/tools/build/cross-build/fgetln_fallback.c
new file mode 100644
index 000000000000..9bd202c125da
--- /dev/null
+++ b/tools/build/cross-build/fgetln_fallback.c
@@ -0,0 +1,84 @@
+/*
+ * Copyright © 2005 Hector Garcia Alvarez
+ * Copyright © 2005, 2008-2012 Guillem Jover <guillem@hadrons.org>
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``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.
+ */
+
+#include <stdio.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+#include <string.h>
+
+#include "local-link.h"
+
+#define HAVE_GETLINE 1
+#ifdef HAVE_GETLINE
+struct filebuf {
+ FILE *fp;
+ char *buf;
+ size_t len;
+};
+
+#define FILEBUF_POOL_ITEMS 32
+
+static struct filebuf fb_pool[FILEBUF_POOL_ITEMS];
+static int fb_pool_cur;
+
+char *
+fgetln(FILE *stream, size_t *len)
+{
+ struct filebuf *fb;
+ ssize_t nread;
+
+ flockfile(stream);
+
+ /* Try to diminish the possibility of several fgetln() calls being
+ * used on different streams, by using a pool of buffers per file. */
+ fb = &fb_pool[fb_pool_cur];
+ if (fb->fp != stream && fb->fp != NULL) {
+ fb_pool_cur++;
+ fb_pool_cur %= FILEBUF_POOL_ITEMS;
+ fb = &fb_pool[fb_pool_cur];
+ }
+ fb->fp = stream;
+
+ nread = getline(&fb->buf, &fb->len, stream);
+
+ funlockfile(stream);
+
+ /* Note: the getdelim/getline API ensures nread != 0. */
+ if (nread == -1) {
+ *len = 0;
+ return NULL;
+ } else {
+ *len = (size_t)nread;
+ return fb->buf;
+ }
+}
+libbsd_link_warning(fgetln,
+ "This function cannot be safely ported, use getline(3) "
+ "instead, as it is supported by GNU and POSIX.1-2008.")
+#else
+#error "Function fgetln() needs to be ported."
+#endif
diff --git a/tools/build/cross-build/fgetwln_fallback.c b/tools/build/cross-build/fgetwln_fallback.c
new file mode 100644
index 000000000000..17254743fbc4
--- /dev/null
+++ b/tools/build/cross-build/fgetwln_fallback.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2012 Guillem Jover <guillem@hadrons.org>
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``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.
+ */
+
+#include <sys/cdefs.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <wchar.h>
+
+#include "local-link.h"
+
+struct filewbuf {
+ FILE *fp;
+ wchar_t *wbuf;
+ size_t len;
+};
+
+#define FILEWBUF_INIT_LEN 128
+#define FILEWBUF_POOL_ITEMS 32
+
+static struct filewbuf fb_pool[FILEWBUF_POOL_ITEMS];
+static int fb_pool_cur;
+
+wchar_t *
+fgetwln(FILE *stream, size_t *lenp)
+{
+ struct filewbuf *fb;
+ wint_t wc;
+ size_t wused = 0;
+
+ /* Try to diminish the possibility of several fgetwln() calls being
+ * used on different streams, by using a pool of buffers per file. */
+ fb = &fb_pool[fb_pool_cur];
+ if (fb->fp != stream && fb->fp != NULL) {
+ fb_pool_cur++;
+ fb_pool_cur %= FILEWBUF_POOL_ITEMS;
+ fb = &fb_pool[fb_pool_cur];
+ }
+ fb->fp = stream;
+
+ while ((wc = fgetwc(stream)) != WEOF) {
+ if (!fb->len || wused >= fb->len) {
+ wchar_t *wp;
+
+ if (fb->len)
+ fb->len *= 2;
+ else
+ fb->len = FILEWBUF_INIT_LEN;
+
+ wp = reallocarray(fb->wbuf, fb->len, sizeof(wchar_t));
+ if (wp == NULL) {
+ wused = 0;
+ break;
+ }
+ fb->wbuf = wp;
+ }
+
+ fb->wbuf[wused++] = wc;
+
+ if (wc == L'\n')
+ break;
+ }
+
+ *lenp = wused;
+ return wused ? fb->wbuf : NULL;
+}
+
+libbsd_link_warning(fgetwln,
+ "This function cannot be safely ported, use fgetwc(3) "
+ "instead, as it is supported by C99 and POSIX.1-2001.")
diff --git a/tools/build/cross-build/local-link.h b/tools/build/cross-build/local-link.h
new file mode 100644
index 000000000000..a7534b22be68
--- /dev/null
+++ b/tools/build/cross-build/local-link.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright © 2015 Guillem Jover <guillem@hadrons.org>
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``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.
+ */
+
+#ifndef LIBBSD_LOCAL_LINK_H
+#define LIBBSD_LOCAL_LINK_H
+
+#ifdef notyet
+#define libbsd_link_warning(symbol, msg) \
+ static const char libbsd_emit_link_warning_##symbol[] \
+ __attribute__((__used__,__section__(".gnu.warning." #symbol))) = msg;
+#else
+#define libbsd_link_warning(symbol, msg)
+#endif
+
+#endif
diff --git a/tools/build/cross-build/progname.c b/tools/build/cross-build/progname.c
new file mode 100644
index 000000000000..645227df48d7
--- /dev/null
+++ b/tools/build/cross-build/progname.c
@@ -0,0 +1,53 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2018-2020 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.
+ *
+ * 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.
+ */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <stdlib.h>
+
+#ifdef __GLIBC__
+extern const char *__progname;
+const char *
+getprogname(void)
+{
+ return (__progname);
+}
+void
+setprogname(const char *progname)
+{
+ __progname = progname;
+}
+#endif /* __GLIBC__ */
diff --git a/tools/build/libc-bootstrap/libc_private.h b/tools/build/libc-bootstrap/libc_private.h
new file mode 100644
index 000000000000..febee84ac24e
--- /dev/null
+++ b/tools/build/libc-bootstrap/libc_private.h
@@ -0,0 +1,40 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2018-2020 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.
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+#pragma once
+
+#define __libc_sigprocmask(a, b, c) sigprocmask(a, b, c)
diff --git a/tools/build/libc-bootstrap/namespace.h b/tools/build/libc-bootstrap/namespace.h
new file mode 100644
index 000000000000..73e27f8cb02b
--- /dev/null
+++ b/tools/build/libc-bootstrap/namespace.h
@@ -0,0 +1,52 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2018-2020 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.
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+#pragma once
+
+#define _open(...) open(__VA_ARGS__)
+#define _close(a) close(a)
+#define _fstat(a, b) fstat(a, b)
+#define _read(a, b, c) read(a, b, c)
+#define _write(a, b, c) write(a, b, c)
+#define _writev(a, b, c) writev(a, b, c)
+#define _fsync(a) fsync(a)
+#define _getprogname() getprogname()
+#define _err(...) err(__VA_ARGS__)
+
+#define _pthread_mutex_unlock pthread_mutex_unlock
+#define _pthread_mutex_lock pthread_mutex_lock
+
diff --git a/tools/build/libc-bootstrap/un-namespace.h b/tools/build/libc-bootstrap/un-namespace.h
new file mode 100644
index 000000000000..398707791792
--- /dev/null
+++ b/tools/build/libc-bootstrap/un-namespace.h
@@ -0,0 +1,40 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2018-2020 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.
+ *
+ * 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.
+ *
+ * $FreeBSD$
+ */
+#pragma once
+/* This can be empty when building the FreeBSD compatible bootstrap files */
+
diff --git a/tools/build/mk/Makefile.boot b/tools/build/mk/Makefile.boot
index 0b4c41b8d95f..1d121e916a71 100644
--- a/tools/build/mk/Makefile.boot
+++ b/tools/build/mk/Makefile.boot
@@ -5,6 +5,74 @@ DPADD+= ${WORLDTMP}/legacy/usr/lib/libegacy.a
LDADD+= -legacy
LDFLAGS+= -L${WORLDTMP}/legacy/usr/lib
+.if ${.MAKE.OS} != "FreeBSD"
+# On MacOS using a non-mac ar will fail the build, similarly on Linux using
+# nm may not work as expected if the nm for the target architecture comes in
+# $PATH before a nm that supports the host architecture.
+# To ensure that host binary compile as expected we use the tools from /usr/bin.
+AR:= /usr/bin/ar
+RANLIB:= /usr/bin/ranlib
+NM:= /usr/bin/nm
+
+# Don't use lorder and tsort since lorder is not installed by default on most
+# Linux systems and the FreeBSD lorder does not work on Linux. For the bootstrap
+# tools the order of the .o files should not matter since we only care about
+# a few individual files (and might soon require linking with lld anyway)
+LORDER:=echo
+TSORT:=cat
+# When using cat as tsort we can't pass -q:
+TSORTFLAGS:=
+
+# Avoid stale dependecy warnings:
+LIBC:=
+LIBZ:=
+LIBM:=
+LIBUTIL:=
+LIBCPLUSPLUS:=
+LIBARCHIVE:=
+LIBPTHREAD:=
+LIBMD:=${WORLDTMP}/legacy/usr/lib/libmd.a
+LIBNV:=${WORLDTMP}/legacy/usr/lib/libmd.a
+LIBSBUF:=${WORLDTMP}/legacy/usr/lib/libsbuf.a
+LIBY:=${WORLDTMP}/legacy/usr/lib/liby.a
+LIBL:=${WORLDTMP}/legacy/usr/lib/libl.a
+LIBROKEN:=${WORLDTMP}/legacy/usr/lib/libroken.a
+LIBDWARF:=${WORLDTMP}/legacy/usr/lib/libdwarf.a
+LIBELF:=${WORLDTMP}/legacy/usr/lib/libelf.a
+
+# Add various -Werror flags to catch missing function declarations
+CFLAGS+= -Werror=implicit-function-declaration -Werror=implicit-int \
+ -Werror=return-type -Wundef
+CFLAGS+= -DHAVE_NBTOOL_CONFIG_H=1
+CFLAGS+= -D__BSD_VISIBLE=1
+CFLAGS+= -I${SRCTOP}/tools/build/cross-build/include/common
+
+# b64_pton and b64_ntop is in libresolv on MacOS and Linux:
+# TODO: only needed for uuencode and uudecode
+LDADD+=-lresolv
+
+.if ${.MAKE.OS} == "Linux"
+CFLAGS+= -I${SRCTOP}/tools/build/cross-build/include/linux
+CFLAGS+= -std=gnu99 -D_GNU_SOURCE=1
+# Needed for sem_init, etc. on Linux (used by usr.bin/sort)
+LDADD+= -pthread
+
+.elif ${.MAKE.OS} == "Darwin"
+CFLAGS+= -D_DARWIN_C_SOURCE=1
+CFLAGS+= -I${SRCTOP}/tools/build/cross-build/include/mac
+# The macOS ar and ranlib don't understand all the flags supported by the
+# FreeBSD and Linux ar/ranlib
+ARFLAGS:= -crs
+RANLIBFLAGS:=
+
+# to get libarchive (needed for elftoolchain)
+# MacOS ships /usr/lib/libarchive.dylib but doesn't provide the headers
+CFLAGS+= -idirafter ${SRCTOP}/contrib/libarchive/libarchive
+.else
+.error "Unsupported build OS: ${.MAKE.OS}"
+.endif
+.endif # ${.MAKE.OS} != "FreeBSD"
+
# we do not want to capture dependencies referring to the above
UPDATE_DEPENDFILE= no
diff --git a/tools/build/mk/Makefile.boot.pre b/tools/build/mk/Makefile.boot.pre
new file mode 100644
index 000000000000..4113c934fbc0
--- /dev/null
+++ b/tools/build/mk/Makefile.boot.pre
@@ -0,0 +1,12 @@
+# $FreeBSD$
+
+# Various MK_* options need to be set before including bsd.prog.mk/bsd.lib.mk
+.if ${.MAKE.OS} != "FreeBSD"
+# Most Linux distributions don't ship the .a files for static linking.
+# And on macOS it is impossible to create a statically linked binary.
+NO_SHARED:= no
+MK_PIE:= no
+MK_RETPOLINE= no
+# There is no objcopy on macOS so we can't do the MK_DEBUG_FILES objcopy magic.
+MK_DEBUG_FILES:=no
+.endif
diff --git a/tools/build/mk/bsd.lib.mk b/tools/build/mk/bsd.lib.mk
index 744f5b5a8b9d..6f717d812696 100644
--- a/tools/build/mk/bsd.lib.mk
+++ b/tools/build/mk/bsd.lib.mk
@@ -1,4 +1,7 @@
# $FreeBSD$
+.include "Makefile.boot.pre"
+# Don't build shared libraries during bootstrap
+NO_PIC= yes
.include "../../../share/mk/bsd.lib.mk"
.include "Makefile.boot"
diff --git a/tools/build/mk/bsd.prog.mk b/tools/build/mk/bsd.prog.mk
index d650665d6200..3eb5c904e2ef 100644
--- a/tools/build/mk/bsd.prog.mk
+++ b/tools/build/mk/bsd.prog.mk
@@ -1,4 +1,5 @@
# $FreeBSD$
+.include "Makefile.boot.pre"
.include "../../../share/mk/bsd.prog.mk"
.include "Makefile.boot"