build: export RUSTC_BOOTSTRAP=1 when NV_RS_KERNEL_IBT=1 (fixes #31)

Stock 580.178.04 nv-kernel.o_binary unconditionally emits endbr64
(f3 0f 1e fa) prologues so CONFIG_X86_KERNEL_IBT=y kernels do not raise
#CP on indirect calls. utils.mk already enables -Z cf-protection=branch
-C jump-tables=no on IBT kernels, but stable/distro rustc (including
rust-toolchain.toml 1.100.0, Arch rust, Gentoo dev-lang/rust) rejected
-Z without RUSTC_BOOTSTRAP=1.

Exporting RUSTC_BOOTSTRAP=1 inside the NV_RS_KERNEL_IBT=1 block allows
stable rustc to emit ENDBR64 prologues directly (verified on stable
rustc 1.100.0: check_kernel_module.py passed=true, largest_stack=1000,
data_relocation_targets_without_endbr=0,
data_relocations_pointing_inside_a_function=0).
This commit is contained in:
Eric Hartford
2026-12-01 05:28:22 +00:00
parent cd60b6d658
commit c5d51ec8f7
3 changed files with 5 additions and 3 deletions

View File

@ -26,5 +26,5 @@ Where stock NVIDIA `580.178.04` avoids `#if LINUX_VERSION_CODE` guards altogethe
- Stock `nv-kernel.o_binary` emits `endbr64` (`f3 0f 1e fa`) at indirect-call/vtable entries and omits it on direct-only internal functions.
- `utils.mk` auto-detects `CONFIG_X86_KERNEL_IBT=y` in `$(KERNEL_OUTPUT)/.config` (overridable via `NV_RS_KERNEL_IBT=1` / `NV_RS_KERNEL_IBT=0`). When enabled on `x86_64`, `RUST_KERNEL_FLAGS` adds `-Z cf-protection=branch -C jump-tables=no` (`-C jump-tables=no` avoids per-function `.rodata.<sym>` switch tables that objtool rejects with `--werror`).
- Because `-Z cf-protection=branch` is an unstable flag, IBT kernel builds should use a pinned nightly toolchain (e.g. `RUSTUP_TOOLCHAIN=nightly-2026-11-29 make modules`), while non-IBT kernels build on the default stable toolchain in `rust-toolchain.toml` (`1.100.0`).
- Because `-Z cf-protection=branch` is an unstable `rustc` flag, `utils.mk` automatically exports `RUSTC_BOOTSTRAP=1` whenever `NV_RS_KERNEL_IBT=1`. This allows both the pinned stable toolchain in `rust-toolchain.toml` (`1.100.0`) and distro-packaged stable `rustc` (Arch, Gentoo, Fedora, etc.) to emit `ENDBR64` prologues without requiring a nightly toolchain.
- `tools/check_kernel_module.py` enforces `endbr64_at_data_relocation_targets` on `x86_64` whenever `CONFIG_X86_KERNEL_IBT=y`, `NV_RS_KERNEL_IBT=1`, or the built object emits `endbr64` entries. Both symbol and section-symbol relocations are resolved against disassembled function-entry addresses so valid function-entry targets pass and mid-function or non-`endbr64` targets fail. ARM64 objects are exempt from the x86 `endbr64` rule.

View File

@ -1,5 +1,5 @@
[toolchain]
channel = "1.100.0" # Default stable toolchain; for CONFIG_X86_KERNEL_IBT=y kernels build with RUSTUP_TOOLCHAIN=nightly-2026-11-29 (needed for -Z cf-protection=branch). See docs/KERNEL_COMPAT.md.
channel = "1.100.0" # Stable toolchain; utils.mk exports RUSTC_BOOTSTRAP=1 automatically for -Z cf-protection=branch on CONFIG_X86_KERNEL_IBT=y kernels. See docs/KERNEL_COMPAT.md.
profile = "minimal"
components = ["rustfmt", "clippy"]
targets = ["x86_64-unknown-none"]

View File

@ -24,10 +24,12 @@ RUST_KERNEL_FLAGS := --edition=2021 --crate-type=lib --target=$(RUST_KERNEL_TARG
# Auto-enable IBT flags when the target kernel has CONFIG_X86_KERNEL_IBT=y (override with NV_RS_KERNEL_IBT=0/1).
# -Z cf-protection=branch emits ENDBR64 on indirect-call targets (matching stock nv-kernel.o_binary);
# -C jump-tables=no avoids .rodata.<fn> switch tables that objtool rejects with CONFIG_OBJTOOL_WERROR=y.
# Requires a nightly toolchain (e.g. RUSTUP_TOOLCHAIN=nightly-2026-11-29).
# Export RUSTC_BOOTSTRAP=1 so stable/distro rustc (and rust-toolchain.toml 1.100.0) accepts
# -Z cf-protection=branch without requiring users to install a nightly toolchain.
NV_RS_KERNEL_IBT ?= $(if $(shell grep -q '^CONFIG_X86_KERNEL_IBT=y$$' $(KERNEL_OUTPUT)/.config 2>/dev/null && echo y),1,0)
ifeq ($(NV_RS_KERNEL_IBT),1)
RUST_KERNEL_FLAGS += -Z cf-protection=branch -C jump-tables=no
export RUSTC_BOOTSTRAP := 1
endif
endif