Toolchain: Add support for building the userland with the mold linker

This commit adds support for building the SerenityOS userland with the
new [mold linker].

This is not enabled by default yet; to link using mold, run the
`Toolchain/BuildMold.sh` script to build the latest release of mold, and
set the `ENABLE_MOLD_LINKER` CMake variable to ON. This option relies on
toolchain support that has been added just recently, so you might need
to rebuild your toolchain for mold to work.

[mold linker]: https://github.com/rui314/mold
This commit is contained in:
Daniel Bertalan 2022-01-19 20:54:34 +01:00 committed by Brian Gianforcaro
parent ad649c48da
commit 4055c393fc
7 changed files with 60 additions and 1 deletions

View file

@ -267,6 +267,9 @@ endif()
add_subdirectory(AK)
add_subdirectory(Kernel)
if(NOT "${SERENITY_ARCH}" STREQUAL "aarch64")
if (ENABLE_MOLD_LINKER)
add_link_options(-fuse-ld=mold)
endif()
add_subdirectory(Userland)
add_subdirectory(Tests)
endif()

View file

@ -53,6 +53,7 @@ There are some optional features that can be enabled during compilation that are
- `ENABLE_PCI_IDS_DOWNLOAD`: downloads the [`pci.ids` database](https://pci-ids.ucw.cz/) that contains information about PCI devices at build time, if not already present. Enabled by default.
- `BUILD_LAGOM`: builds [Lagom](../Meta/Lagom/ReadMe.md), which makes various SerenityOS libraries and programs available on the host system.
- `ENABLE_KERNEL_LTO`: builds the kernel with link-time optimization.
- `ENABLE_MOLD_LINKER`: builds the userland with the [`mold` linker](https://github.com/rui314/mold). `mold` can be built by running `Toolchain/BuildMold.sh`.
- `INCLUDE_WASM_SPEC_TESTS`: downloads and includes the WebAssembly spec testsuite tests. In order to use this option, you will need to install `prettier` and `wabt`. wabt version 1.0.23 or higher is required to pre-process the WebAssembly spec testsuite.
- `SERENITY_TOOLCHAIN`: Specifies whether to use the established GNU toolchain, or the experimental Clang-based toolchain for building SerenityOS. See the [Clang-based toolchain](#clang-based-toolchain) section below.
- `SERENITY_ARCH`: Specifies which architecture to build for. Currently supported options are `i686` and `x86_64`. `x86_64` requires a separate toolchain build from `i686`.

View file

@ -11,3 +11,4 @@ serenity_option(ENABLE_KERNEL_ADDRESS_SANITIZER OFF CACHE BOOL "Enable kernel ad
serenity_option(ENABLE_KERNEL_COVERAGE_COLLECTION OFF CACHE BOOL "Enable KCOV and kernel coverage instrumentation in gcc/clang")
serenity_option(ENABLE_KERNEL_LTO OFF CACHE BOOL "Build the kernel with link-time optimization")
serenity_option(ENABLE_EXTRA_KERNEL_DEBUG_SYMBOLS OFF CACHE BOOL "Enable -Og and -ggdb3 options for Kernel code for easier debugging")
serenity_option(ENABLE_MOLD_LINKER OFF CACHE BOOL "Link the SerenityOS userland with the mold linker")

View file

@ -57,7 +57,9 @@ function(serenity_libc target_name fs_name)
target_link_libraries(${target_name} clang_rt.builtins)
# FIXME: Implement -static-libstdc++ in the next toolchain update.
target_link_options(${target_name} PRIVATE -nostdlib++ -Wl,-Bstatic -lc++ -Wl,-Bdynamic)
target_link_options(${target_name} PRIVATE -Wl,--no-dependent-libraries)
if (NOT ENABLE_MOLD_LINKER)
target_link_options(${target_name} PRIVATE -Wl,--no-dependent-libraries)
endif()
endif()
target_link_directories(LibC PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
serenity_generated_sources(${target_name})

View file

@ -294,6 +294,10 @@ pushd "$DIR/Build/clang"
done
popd
pushd "$DIR/Local/clang/bin/"
buildstep "mold_symlink" ln -s ../../mold/bin/mold ld.mold
popd
# === SAVE TO CACHE ===
pushd "$DIR"

View file

@ -399,6 +399,10 @@ pushd "$DIR/Build/$ARCH"
popd
pushd "$DIR/Local/$ARCH/$ARCH-pc-serenity/bin"
buildstep "mold_symlink" ln -s ../../../mold/bin/mold ld.mold
popd
# == SAVE TO CACHE ==

44
Toolchain/BuildMold.sh Executable file
View file

@ -0,0 +1,44 @@
#!/usr/bin/env bash
# This script builds the mold linker that can optionally be used for linking
# the SerenityOS userland.
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
NPROC="nproc"
SYSTEM_NAME="$(uname -s)"
if [ "$SYSTEM_NAME" = "OpenBSD" ]; then
NPROC="sysctl -n hw.ncpuonline"
elif [ "$SYSTEM_NAME" = "FreeBSD" ]; then
NPROC="sysctl -n hw.ncpu"
elif [ "$SYSTEM_NAME" = "Darwin" ]; then
NPROC="sysctl -n hw.ncpu"
fi
[ -z "$MAKEJOBS" ] && MAKEJOBS=$($NPROC)
mkdir -p "$DIR"/Tarballs
pushd "$DIR"/Tarballs
if [ "$1" = "--git" ]; then
[ ! -d mold ] && git clone https://github.com/rui314/mold.git
cd mold
git pull
else
VERSION=1.0.2
[ ! -e mold-$VERSION.tar.gz ] && curl -L "https://github.com/rui314/mold/archive/refs/tags/v$VERSION.tar.gz" -o mold-$VERSION.tar.gz
[ ! -e mold-$VERSION ] && tar -xzf mold-$VERSION.tar.gz
cd mold-$VERSION
fi
make clean
export DESTDIR="$DIR"/Local/mold
export PREFIX=
make -j "$MAKEJOBS"
make install
popd