Toolchains

Document Revision: 26h1.0


1. Clang (cmake/toolchain/zxfoundation-clang.cmake)

Uses LLVM's built-in cross-compilation support — no separate cross-compiler installation is required on most systems.

cmake -B build \
  -DCMAKE_TOOLCHAIN_FILE=cmake/toolchain/zxfoundation-clang.cmake \
  -DMARCH_MODE=z14
RoleTool
C compilerclang (or clang-$CLANG_VERSION)
Linkerld.lld
Archiverllvm-ar
objcopyllvm-objcopy
Host CCclang

Set CLANG_VERSION in the environment to select a versioned binary (e.g. CLANG_VERSION=18clang-18). If unset, unversioned clang is used.

The target triple --target=s390x-unknown-none-elf is passed as a compile option (not via CMAKE_C_COMPILER_TARGET) to avoid CMake's compiler detection interfering with the freestanding build.


2. GCC (cmake/toolchain/zxfoundation-gcc.cmake)

Requires a s390x-ibm-linux-gnu-* cross-compiler toolchain installed on the host.

cmake -B build \
  -DCMAKE_TOOLCHAIN_FILE=cmake/toolchain/zxfoundation-gcc.cmake
RoleTool
C compilers390x-ibm-linux-gnu-gcc
Linkers390x-ibm-linux-gnu-ld
Archivers390x-ibm-linux-gnu-ar
objcopys390x-ibm-linux-gnu-objcopy
Host CCgcc

GCC-specific flags added to the kernel target:

FlagReason
-static-libgccAvoid libgcc DSO dependency
-Wno-array-boundsSuppress false positives from GCC's array-bounds analysis on lowcore pointer casts
-fno-delete-null-pointer-checksThe kernel legitimately dereferences physical address 0x0 (the lowcore)
-mzarchForce z/Architecture mode

3. Common Compiler Flags

Applied to all targets (loader and kernel):

FlagReason
-ffreestandingNo hosted C library assumptions
-nostdlibNo implicit library linking
-fno-builtinPrevent compiler from substituting builtins with libc calls
-fno-strict-aliasingKernel code casts between unrelated pointer types
-fwrapvSigned integer overflow wraps (defined behavior)
-ftrivial-auto-var-init=patternAuto-initialize locals to a poison pattern — catches use-before-init
-fno-stack-protectorNo __stack_chk_guard — freestanding, no libc
-msoft-floatNo FPU use in kernel
-mno-vxNo vector instructions in kernel

Kernel-only additional flag:

FlagReason
-mpacked-stackUse packed register save areas (reduces stack frame size)

4. Custom Toolchain

To use a non-standard toolchain, copy one of the provided toolchain files and adjust the compiler/linker paths. The following CMake variables must be set:

VariableDescription
CMAKE_C_COMPILERPath to the C compiler
CMAKE_LINKERPath to the linker
CMAKE_OBJCOPYPath to objcopy
ZX_HOST_CCHost C compiler for building bin2rec and zxsign
COMPILER_ID"clang" or "gcc" (selects compiler-specific flag sets)
TARGET_EMULATION_MODEelf64_s390
MARCH_MODETarget microarchitecture (e.g. z10, z14, z16)