Skip to content

Instantly share code, notes, and snippets.

@bazad
Created October 6, 2017 21:24
Show Gist options
  • Save bazad/654959120a423b226dc564073b435453 to your computer and use it in GitHub Desktop.
Save bazad/654959120a423b226dc564073b435453 to your computer and use it in GitHub Desktop.
A script to build XNU version 4570.1.46 (macOS High Sierra 10.13).
#! /bin/bash
#
# build-xnu-4570.1.46.sh
# Brandon Azad
#
# A script showing how to build XNU version 4570.1.46 on MacOS High Sierra
# 10.13 with Xcode 9.
#
# Note: This process will OVERWRITE files in Xcode's MacOSX10.13.sdk. Make a
# backup of this directory first!
#
# Set the working directory.
WORKDIR="${WORKDIR:-build-xnu-4570.1.46}"
# Set a permissive umask just in case.
umask 022
# Print commands and exit on failure.
set -ex
# Get the SDK path and toolchain path.
SDKPATH="$(xcrun --sdk macosx --show-sdk-path)"
TOOLCHAINPATH="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain"
[ -d "${SDKPATH}" ] && [ -d "${TOOLCHAINPATH}" ]
# Create the working directory.
mkdir "${WORKDIR}"
cd "${WORKDIR}"
# Back up the SDK if that option is given.
if [ -n "${BACKUP_SDK}" ]; then
sudo ditto "${SDKPATH}" "$(basename "${SDKPATH}")"
fi
# Download XNU and some additional sources we will need to help build.
curl https://opensource.apple.com/tarballs/xnu/xnu-4570.1.46.tar.gz | tar -xf-
curl https://opensource.apple.com/tarballs/dtrace/dtrace-262.tar.gz | tar -xf-
curl https://opensource.apple.com/tarballs/AvailabilityVersions/AvailabilityVersions-32.tar.gz | tar -xf-
curl https://opensource.apple.com/tarballs/libplatform/libplatform-161.tar.gz | tar -xf-
curl https://opensource.apple.com/tarballs/libdispatch/libdispatch-913.1.6.tar.gz | tar -xf-
# Build and install ctf utilities. This adds the ctf tools to
# ${TOOLCHAINPATH}/usr/local/bin.
cd dtrace-262
mkdir -p obj dst sym
xcodebuild install -target ctfconvert -target ctfdump -target ctfmerge ARCHS="x86_64" SRCROOT="${PWD}" OBJROOT="${PWD}/obj" SYMROOT="${PWD}/sym" DSTROOT="${PWD}/dst"
# TODO: Get the XcodeDefault.toolchain path programmatically.
sudo ditto "${PWD}/dst/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain" "${TOOLCHAINPATH}"
cd ..
# Install AvailabilityVersions. This writes to ${SDKPATH}/usr/local/libexec.
cd AvailabilityVersions-32
mkdir -p dst
make install SRCROOT="${PWD}" DSTROOT="${PWD}/dst"
sudo ditto "${PWD}/dst/usr/local" "${SDKPATH}/usr/local"
cd ..
# Install the XNU headers we'll need for libdispatch. This OVERWRITES files in
# MacOSX10.13.sdk!
cd xnu-4570.1.46
mkdir -p BUILD.hdrs/obj BUILD.hdrs/sym BUILD.hdrs/dst
make installhdrs SDKROOT=macosx ARCH_CONFIGS=X86_64 SRCROOT="${PWD}" OBJROOT="${PWD}/BUILD.hdrs/obj" SYMROOT="${PWD}/BUILD.hdrs/sym" DSTROOT="${PWD}/BUILD.hdrs/dst"
# HACK: The subsequent build command fails with a missing file error. We create
# that file so that the build continues.
touch libsyscall/os/thread_self_restrict.h
xcodebuild installhdrs -project libsyscall/Libsyscall.xcodeproj -sdk macosx ARCHS="x86_64 i386" SRCROOT="${PWD}/libsyscall" OBJROOT="${PWD}/BUILD.hdrs/obj" SYMROOT="${PWD}/BUILD.hdrs/sym" DSTROOT="${PWD}/BUILD.hdrs/dst"
# Set permissions correctly before dittoing over MacOSX10.13.sdk.
sudo chown -R root:wheel BUILD.hdrs/dst/
sudo ditto BUILD.hdrs/dst "${SDKPATH}"
cd ..
# Install libplatform headers to ${SDKPATH}/usr/local/include.
cd libplatform-161
sudo ditto "${PWD}/include" "${SDKPATH}/usr/local/include"
sudo ditto "${PWD}/private" "${SDKPATH}/usr/local/include"
cd ..
# Build and install libdispatch's libfirehose_kernel target to
# ${SDKPATH}/usr/local.
cd libdispatch-913.1.6
mkdir -p obj sym dst
xcodebuild install -project libdispatch.xcodeproj -target libfirehose_kernel -sdk macosx ARCHS="x86_64 i386" SRCROOT="${PWD}" OBJROOT="${PWD}/obj" SYMROOT="${PWD}/sym" DSTROOT="${PWD}/dst"
sudo ditto "${PWD}/dst/usr/local" "${SDKPATH}/usr/local"
cd ..
# Build XNU.
cd xnu-4570.1.46
make SDKROOT=macosx ARCH_CONFIGS=X86_64 KERNEL_CONFIGS="RELEASE"
@antoniofrighetto
Copy link

@bazad Modified and extended your script so that any XNU version can be built (at least as of 10.13.3).

@below
Copy link

below commented Jul 25, 2018

This seems to fail with 10.13.2 and later.
One of the issues is that cpu_topology.c wants ptrdiff_t (defined in bsd/sys/_types/_ptrdiff_t.h).

There are some other issues, so has anyone successfully built 10.13.2 or later?

@venkat1224
Copy link

How To get the "file structure/ fd" in open of character device in Mac driver

With the parameter which I get in ".open/.close" unable to distinguish from which file descriptor the method is being called, can you please let me know the procedure to retrieve the the info so that It can be uniquely identified.

Is there any other alternatives to character device, so that I can establish the connection between user-space and kernel space
typedef int (dev_t, int, int, struct proc *) open_close_fcn_t;
struct cdevsw {
open_close_fcn_t *d_open;
open_close_fcn_t *d_close;
read_write_fcn_t *d_read;
read_write_fcn_t *d_write;
....
}

I have tried to fetch the filedesc structure from "struct proc / proc_self()" so that I can get the info of all open file descriptors corresponding to process
struct filedesc *fdp = p->p_fd;
but able to see "*fpd" as NULL

Thanks
Venkat

@venkat1224
Copy link

venkat1224 commented Aug 2, 2018

*sorry ignore the above comment, I was supposed to post in other page, but if you have the solution please let me know

@aboulfad
Copy link

aboulfad commented Oct 4, 2018

dtrace-262 compile: could not find included file 'common.xcconfig' in search paths
I can not get past compiling and installing dtrace, executing the line xcodebuild install -target ctfconvert -target ctfdump -target ctfmerge ARCHS="x86_64" SRCROOT="${PWD}" OBJROOT="${PWD}/obj" SYMROOT="${PWD}/sym" DSTROOT="${PWD}/dst" yields the above error.

The .xconfig files in config dir include that file, where is it supposed to be located at ? thanks.

Info:
MacOs 10.14 (Mojave)
Xcode 10

@afrojer
Copy link

afrojer commented Dec 20, 2018

For mojave, I've update my blog: https://kernelshaman.blogspot.com
And Makefile: https://jeremya.com/sw/Makefile.xnudeps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment