From 4055c393fc996cf087bda3683b206594bb32bcb6 Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Wed, 19 Jan 2022 20:54:34 +0100 Subject: [PATCH] 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 --- CMakeLists.txt | 3 ++ Documentation/AdvancedBuildInstructions.md | 1 + Meta/CMake/serenity_options.cmake | 1 + Meta/CMake/utils.cmake | 4 +- Toolchain/BuildClang.sh | 4 ++ Toolchain/BuildIt.sh | 4 ++ Toolchain/BuildMold.sh | 44 ++++++++++++++++++++++ 7 files changed, 60 insertions(+), 1 deletion(-) create mode 100755 Toolchain/BuildMold.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index db125297306..e43790090a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/Documentation/AdvancedBuildInstructions.md b/Documentation/AdvancedBuildInstructions.md index 10c27cf4b7b..2747d0a03e2 100644 --- a/Documentation/AdvancedBuildInstructions.md +++ b/Documentation/AdvancedBuildInstructions.md @@ -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`. diff --git a/Meta/CMake/serenity_options.cmake b/Meta/CMake/serenity_options.cmake index 7c74bf1a089..a6128368523 100644 --- a/Meta/CMake/serenity_options.cmake +++ b/Meta/CMake/serenity_options.cmake @@ -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") diff --git a/Meta/CMake/utils.cmake b/Meta/CMake/utils.cmake index 94c90a4b9ff..caee954fcd6 100644 --- a/Meta/CMake/utils.cmake +++ b/Meta/CMake/utils.cmake @@ -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}) diff --git a/Toolchain/BuildClang.sh b/Toolchain/BuildClang.sh index 160c41d766c..da940b9f434 100755 --- a/Toolchain/BuildClang.sh +++ b/Toolchain/BuildClang.sh @@ -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" diff --git a/Toolchain/BuildIt.sh b/Toolchain/BuildIt.sh index 127a982ab4c..186c48bf82e 100755 --- a/Toolchain/BuildIt.sh +++ b/Toolchain/BuildIt.sh @@ -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 == diff --git a/Toolchain/BuildMold.sh b/Toolchain/BuildMold.sh new file mode 100755 index 00000000000..75f427b3e52 --- /dev/null +++ b/Toolchain/BuildMold.sh @@ -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