Building Rust Nightly on OpenBSD
OpenBSD has tier 3 support in Rust.
Rust organizes its platforms in three tiers. Tier 1 (Linux, macOS, Windows) has automated builds, full tests and official binaries. Tier 2 has automated builds and available binaries, but no guarantee that the tests pass. Tier 3 is the lowest tier: the code exists in the repository but there are no automated builds, no CI tests and no distributed binaries. Support depends on the community. OpenBSD is in tier 3.
In practice this means the Rust project does not provide prebuilt binaries
via rustup. If you want a nightly, you need to build it on your own
machine.
The build-rust project by Sebastien Marie, who maintains the Rust port on OpenBSD, automates this process: it uses the stable rustc from the OpenBSD package as bootstrap to build the beta, and then uses the beta to build the nightly. The flow is:
stable (package) -> beta -> nightly
Rust compiles itself. Each new version needs the previous version as the
"bootstrap compiler". The build.sh script automates this with ./build.sh nightly.
In theory it should be simple. In practice I hit 6 problems before
getting a working rustc 1.96.0-nightly on OpenBSD 7.9-current (amd64).
Problem 1: Version gap
The beta source (1.95.0-beta.5) requires rustc 1.94.0 as bootstrap. However the OpenBSD package ships rustc 1.93.1. The build failed immediately:
Unexpected rustc version: 1.93.1, we should use 1.94.x/1.95.0
The solution was to build rustc 1.94.0 from source as an intermediate step, using the system 1.93.1 as bootstrap. The 1.94.0 source accepts 1.93.0+ as bootstrap. The full chain became:
1.93.1 (package) -> 1.94.0 (source) -> 1.95.0-beta -> 1.96.0-nightly
I downloaded the 1.94.0 source from static.rust-lang.org and built it with the
same x.py infrastructure, using a config.toml pointing to the system rustc
and cargo.
Problem 2: LLVM 21
The nightly source (1.96.0) requires LLVM >= 21. OpenBSD 7.9 ships LLVM 20 as the default. The build failed with:
bad LLVM version: 20.1.8, need >=21
I installed the llvm-21.1.8p2 package and configured build-rust to use it in
~/.build_rust.conf:
llvm_config=/usr/local/bin/llvm-config-21
The beta (1.95.0) works with LLVM 20, so this setting only affects the nightly.
Problem 3: IBT and SIGILL in build scripts
This was the most subtle problem and the one that took the most time.
OpenBSD on amd64 uses Intel CET (Control-flow Enforcement Technology), specifically IBT (Indirect Branch Tracking).
An indirect branch is a jump or function call whose target address is determined at run time, not at compile time. For example, calling a function through a pointer (
call rax) or using a vtable. Attackers exploit this to redirect the execution flow to malicious code (techniques like ROP/JOP).IBT mitigates this by requiring every indirect branch target to start with the
endbr64instruction. This instruction does nothing other than signal to the processor: "this is a valid entry point". If the jump target does not start withendbr64, the CPU raises an exception that the kernel converts into SIGILL (signal 4, illegal instruction).
With IBT enabled, every indirect branch target must start with endbr64.
If it does not, the CPU raises SIGILL.
The OpenBSD Rust port includes a patch that makes rustc emit
-Z cf-protection=branch by default, inserting endbr64 in all generated
code. The system rustc 1.93.1 already includes this patch.
Why it happens
During bootstrap the sequence is:
- The system rustc (with cf-protection) builds the stage1 compiler
- The stage1 compiler builds build scripts (small helper programs)
- These build scripts are executed during the build
The build scripts are linked by the C compiler (cc) and the resulting binaries
are subject to IBT enforcement by the OpenBSD kernel. However the Rust
bootstrap system controls its own linker flags internally and does not
pass -z nobtcfi (the OpenBSD linker flag that disables IBT enforcement)
to the build script binaries. Result: the build scripts crash with SIGILL
at runtime.
process didn't exit successfully: `.../build-script-build`
(signal: 4, SIGILL: illegal instruction)
The crash happened specifically in std::rt::lang_start_internal -- the
first function called in any Rust binary. The function had no endbr64
at the entry point because the standard library linked into the build script was
built without cf-protection.
Solution
I modified the C compiler wrapper scripts used during the build to always
pass -Wl,-z,nobtcfi to the linker. build-rust creates wrappers in
build_dir/bin/ for cc and c++:
#!/bin/sh
exec ccache /usr/bin/cc -Wl,-z,nobtcfi "${@}"
This ensures that all binaries produced during the build -- including build scripts that the Rust bootstrap creates internally -- have IBT enforcement disabled.
Additionally, the cf-protection=branch patch from OpenBSD ports must be
applied to the intermediate 1.94.0 build so that the compiler generates endbr64
in Rust code. Without this patch the generated code has no endbr64 and will crash
on systems with IBT enabled (which is the default on OpenBSD 7.9).
The combination of both is required:
- cf-protection=branch: generates
endbr64in compiled Rust code - -Wl,-z,nobtcfi: disables IBT enforcement at the binary level
This mirrors what the OpenBSD ports infrastructure does: bsd.port.mk
adds -z nobtcfi to _LINKER_FLAGS for ports that need it.
I also added RUSTFLAGS globally in the script:
RUSTFLAGS="-C link-arg=-Wl,-z,nobtcfi"
export RUSTFLAGS
Problem 4: vendored libgit2 and header conflicts
cargo depends on libgit2-sys, which can use the system libgit2 or build
its vendored copy. The script originally used
LIBGIT2_SYS_USE_PKG_CONFIG=1 to use the system library.
My first attempt at using the vendored copy required libssh2.h (from the
libssh2 package), which lives in /usr/local/include. When adding
-I/usr/local/include globally to CFLAGS, lzma-sys also tried to use
the system lzma.h, and the structs differed from the vendored version. The build
broke again.
The solution was to adopt the same approach as OpenBSD ports:
LIBGIT2_NO_PKG_CONFIG=1
LIBSSH2_NO_PKG_CONFIG=1
export LIBGIT2_NO_PKG_CONFIG LIBSSH2_NO_PKG_CONFIG
This tells libgit2-sys and libssh2-sys to use their vendored copies without
querying pkg-config, avoiding search path conflicts entirely.
Problem 5: Memory limits
The Rust compiler is big. Building cargo with the freshly built rustc caused an abort with SIGABRT because of the default data segment limit on OpenBSD (1.5 GB):
rustc exited with signal: 6 (SIGABRT) (core dumped)
build.sh already handles this with ulimit -d $(ulimit -dH) in ksh, but when
invoking manually you need to raise the limit:
ulimit -d 8388608 # 8 GB
Problem 6: Vendored crate checksums
The Rust source tree includes vendored dependencies with checksums in
.cargo-checksum.json files. Any patch to vendored code (openssl-sys,
libffi-sys, psm, libgit2-sys) causes a checksum error:
error: the listed checksum of `.../vendor/psm-0.1.26/src/arch/x86_64.s`
has changed
The solution is to clear the checksums of any patched crate:
sed -i 's/"files":{[^}]*}/"files":{}/' vendor/CRATE/.cargo-checksum.json
OpenBSD ports do this globally for all vendor crates in
post-patch:
sed -i 's/"files":{[^}]*}/"files":{}/' ${WRKSRC}/vendor/*/.cargo-checksum.json
Applied patches
I applied the following patches from /usr/ports/lang/rust/patches/ to the
intermediate 1.94.0 build:
- bootstrap optimization - lets optimization flags (
-O) pass through - LLVM library suffixes - correctly parses
.so.X.Ysuffixes on OpenBSD - psm endbr64 - adds the
endbr64instruction to the psm assembly (stack manipulation), required for IBT on amd64 - cf-protection=branch - makes
-Z cf-protection=branchthe default on amd64 - current_exe() - fixes
current_exe()on OpenBSD - libgit2-sys - forces use of the vendored version
- openssl-sys - accepts the newer LibreSSL version from OpenBSD -current
- libffi-sys - forces use of the system libffi because of the OpenBSD W^X policy
Result
$ rustc -vV
rustc 1.96.0-nightly (fb27476aa 2026-03-28) (built from a source tarball)
host: x86_64-unknown-openbsd
LLVM version: 21.1.8