I got a couple of questions at this point on how Rust BPF would work, and whether there’s a way to actually try something out. This post answers part of the first question: how the rust-bpf compiler PoC differs from plain rustc. The second will get a post of its own; the code is at github.com/4ast/rust-bpf.
The thing worth internalizing up front: there is no forked compiler. rust-bpf runs the same unmodified upstream rustc you’d use for any Cargo project, and stops it at LLVM IR. From there a short pipeline takes over: pull the libraries in, lower CO-RE accesses, optimize, patch the IR into something the BPF backend and libbpf accept, and hand it to stock llc. The sections below follow that pipeline — the right-hand side of the following diagram, top to bottom.
Where the roads fork
A normal cargo build has rustc run LLVM codegen itself and hands the .o files to the system linker. rust-bpf stops rustc at LLVM IR instead. The repo’s examples are two real sched_ext schedulers, scx_simple and scx_cosmos. Here’s the compile line for scx_simple (abridged; the Makefile also passes the btf externs and a few -Z flags):
RUSTC_BOOTSTRAP=1 rustc --target bpfel-unknown-none-v4.json --crate-type rlib \
--sysroot=/dev/null -L bld_deps \
-C opt-level=3 -C panic=unwind -C debuginfo=2 \
--emit=llvm-bc -o bld/scx_simple.bc scx_simple.rsTwo things there are load-bearing. --sysroot=/dev/null means there is no prebuilt standard library to fall back on; core and alloc come out of bld_deps, which the Makefile builds. And the target is a local JSON, not a built-in. (RUSTC_BOOTSTRAP=1 just lets a stable rustc accept the nightly-only features and flags all this needs.)
Now, upstream rustc does ship a BPF target. bpfel-unknown-none has been tier-3 for years, and it already sets obj-is-bitcode: true — the flag that makes everything downstream possible. It makes rustc emit LLVM IR where it would normally emit machine code, so every compiled library, core and alloc included, comes out as LLVM IR that can later be merged with the program into one module.
What it doesn’t give you is cpu v4 (the newest BPF instruction set), panic=unwind (more on that below), or atomics: with atomic-cas: false and min-atomic-width: 64, Rust offers no atomic types on it at all. No AtomicBool, no fetch_add — and a counter or a flag that several CPUs update at once is everyday BPF. So we carry a 20-line target spec instead.
No prebuilt libraries
Rust’s standard library comes in layers. core is the bottom one: Option, Result, slices, iterators, atomics, formatting — nothing that needs an operating system or an allocator. alloc adds Box, Vec and String on top, given an allocator. std, which assumes an OS, is out of the picture for BPF.
rustup, the tool that installs Rust toolchains, also ships a prebuilt standard library for each supported target — but not for tier-3 ones like this. So the Makefile builds core and alloc from source (the rust-src component) against our target spec. The compiler and that source have to come from the same toolchain: core leans on compiler internals that only the exactly matching rustc knows about.
Two small pieces are filled in by hand. compiler_builtins, the crate that normally supplies the helper routines compiled code calls behind your back, is a one-line stub the Makefile echoes into rustc; all it defines is a marker symbol alloc expects. The one helper we do need, __multi3 (128-bit multiply, which the BPF backend can’t do on its own), is hand-written LLVM IR in multi3.ll.
Gluing it together
BPF has no dynamic linking, and libbpf’s static linker works on BPF objects, not Rust libraries, so everything is merged into one LLVM module first:
for i in 1 2 3 4 5; do
llvm-link --only-needed prog.bc $(find bld_deps/extracted -name ‘*.rcgu.o’) -o prog.bc
doneYes, five times. --only-needed copies in only what’s already referenced, and llvm-link reads the files once, in order — so when code pulled from a later file needs something from an earlier one, that waits for the next round. A transitive closure by brute force: both schedulers settle after two rounds, and five leaves margin. Not pretty, but fine for a PoC. multi3.bc goes in afterwards, once and without --only-needed.
CO-RE without compiler support
CO-RE (compile once, run everywhere) is what lets one BPF object read kernel struct fields on kernels where those fields sit at different offsets: the compiler records which field the code means, and libbpf patches in the right offset at load time. clang does this natively; rustc doesn’t, yet. So #[btf], a proc macro, makes each field access a call to a placeholder (__btf_field_byte_offset, __btf_field_exists), and bpf-postproc, a small tool built against the same LLVM as rustc, swaps each placeholder for the IR clang would have produced. From there the stock BPF backend emits real relocations — same destination as clang, different road.
That part has an expiry date. RFC 3966 and its implementation, rust-lang/rust#161107, both still open, add the same field queries to rustc itself. Once they land and get a user-facing API, bpf-postproc has nothing left to do and #[btf] becomes a thin layer on top. They cover fields only, though; type and enum relocations are still open.
opt
opt runs next. llvm-link --only-needed has already left most of core behind (scx_simple pulls in 28 of core’s 536 functions); opt deals with the rest. It marks everything except the struct_ops entry points and _LICENSE as internal, so LLVM knows nothing outside the module can call it, then runs the standard -O2 pipeline, which inlines what it can and deletes whatever nobody calls any more. scx_simple goes from 47 functions to 32, and what survives on its own is mostly panic and formatting code.
One flag rides along: --force-remove-attribute=cold, which strips the cold marking Rust puts on panic paths. Take it out and the build still succeeds, but llc moves every cold function into a section of its own, .text.unlikely., and libbpf only accepts a program’s own internal functions in .text — it refuses to open the object at all (program ‘…unwrap_failed’ is static and not supported). Clang would do the same to a C function marked __attribute__((cold)); BPF C just never has those, while Rust’s core marks every panic path cold.
add_ksyms.py, or: making Rust IR legal BPF IR
This started as “put extern declarations in .ksyms so llc emits BTF FUNC entries” and grew into the pass where every Rust-ism BPF can’t represent gets fixed.
kfunc prototypes. kfuncs are kernel functions exported for BPF programs to call. libbpf checks each one a program uses against the kernel’s own prototype, taken from BTF, the kernel’s type information: same number of arguments, same kinds of types. rustc emits no type information for extern declarations, so add_ksyms writes it. By default it derives the prototype from the IR (integers stay integers, pointers become void*), which is enough for kfuncs that take only scalars. Point KSYM_BTF_FILES and BPFTOOL at vmlinux and any module .ko files, and it copies the real prototype out of the kernel’s BTF instead, so struct pointers match too. That’s the same BTF bpftool turns into vmlinux.h, used the other way round: nothing is generated for the Rust code, which still declares its kfuncs and kernel structs by hand. A vmlinux.h for Rust, bindings generated from BTF, is still missing (Aya’s aya-tool does something along those lines).
memcpy/memcmp. BPF C mostly gets away without them because it copies and compares things whose size is known at compile time: clang unrolls __builtin_memcpy and __builtin_memcmp into plain loads, stores and compares, up to about 1KB. Give it a length only known at run time and memcpy or memset is a compile error (”A call to built-in function ‘memcpy’ is not supported”), while memcmp quietly becomes a call to an extern memcmp that libbpf can’t resolve. C programs write a bounded loop instead. Rust can’t dodge it: a slice carries its length at run time, so copy_from_slice, a BTreeMap insert shifting keys along, and == on slices all end up in memcpy, memmove and memcmp with a run-time length.
The kernel exports no memcpy or memcmp kfunc, so add_ksyms redirects every memcpy, memmove and memcmp call to the bpf_arena_memcpy and bpf_arena_memcmp kfuncs — even constant-length copies llc could have unrolled — and renames the debug info along with the symbol, since that’s where llc takes the BTF name from. memset is left alone: llc expands constant-length memsets inline. Neither of those two kfuncs is in bpf-next yet.
BTF linkage. Rust’s debug info doesn’t say which functions are private to the object, so llc records every one of them in BTF as global. The verifier checks a global function on its own, against its BTF signature, and that falls apart as soon as the optimizer drops an unused argument from an internal function (#[track_caller]’s hidden &Location is the usual one). add_ksyms marks internal functions as local, so they’re static in BTF and the verifier checks them as part of their caller.
Every subprogram ends in exit. The verifier rejects a subprogram whose last instruction isn’t an exit or a jump, even when that instruction is a call to bpf_throw, which never returns. Rust panic paths end in unreachable, LLVM’s “control never gets here” marker, so add_ksyms rewrites every unreachable into a ret.
Unwinding: ripped out, or kept
When a Rust function panics, or a BPF program calls bpf_throw(), the stack has to be unwound, and on the way out each frame should give back what it holds: a lock, an RCU read section, a reference. In Rust that give-back code is the Drop glue, and the compiler already emits it as landing pads — that’s what panic=unwind buys.
LLVM 23 teaches the BPF backend to keep those landing pads and tell the kernel where they are, in a .bpf_cleanup section, so bpf_throw() can run them frame by frame. With an older LLVM, or when a program doesn’t need it, add_ksyms cuts the unwinding paths out before llc and objcopy strips the leftover unwind tables afterwards. That’s the default, and it’s what the schedulers use.
exc_cleanup.rs, contributed by Yonghong Song, tests the new path. The call chain is entry → foo1 → foo2 → foo3; foo2 and foo3 each take something the verifier tracks (an RCU read section, disabled preemption), and foo3 throws. If the kernel doesn’t run both cleanups on the way out, the verifier sees a resource that was never released and rejects the program, so the test can’t pass by accident. The C version of the same test needs hand-written assembly; in Rust the cleanup is just Drop.
It takes LLVM 23 plus two add_ksyms switches: KEEP_INVOKE=1 keeps the landing pads, and KERNEL_BTF=1 turns Rust type names like NonNull<u8> into names the kernel accepts. The kernel side, the .bpf_cleanup handling and the bpf_unwind_resume kfunc, isn’t in bpf-next yet.
llc
llc -march=bpfel -mcpu=v4 -filetype=objThat’s the stock LLVM BPF backend, no patches. Out comes bld/scx_simple.o with instructions, BTF and .maps — fully self-contained.
Custom code in all of that: bpf-postproc, add_ksyms.py, the btf runtime crate, the btf-macros proc macro, multi3.ll, the compiler_builtins stub, and the target JSON. Everything else is upstream rustc and upstream LLVM.
Who guarantees safety
Worth stating because it’s the part that isn’t a compiler detail. A native binary is trusted at runtime: the loader maps it, the MMU contains it, and when it’s wrong you get a SIGSEGV. A BPF object is never trusted that way — it has to be proved safe before it runs. The verifier statically checks memory safety and termination at load, then JITs. It does the MMU’s job, ahead of time.
Which is a slightly odd place for Rust to end up: borrowck proves memory safety of safe code at compile time, the verifier proves it again at load time, and neither trusts the other. That’s not wasted work: borrowck catches a whole class of bugs while you’re still writing code instead of as a verifier rejection at load time, and the kernel still trusts nothing from userspace, compiler included.
Between two communities
Rust on BPF already has two homes, and this work sits between them. Aya built the Rust-first path: a pure-Rust loader, the aya-ebpf crates, and bpf-linker, which rustc’s own platform-support docs name as the linker for its BPF targets. If you write BPF in Rust today, you are almost certainly on Aya’s toolchain.
The kernel BPF community comes at it from the other end: libbpf, the verifier, kfuncs, arenas, sched_ext, and the LLVM BPF backend that clang users rely on. rust-bpf lives here: libbpf as the loader, kernel BTF as the source of truth, the same LLVM backend as C.
Its question is whether Rust can reach parity with C for kernel-facing BPF (CO-RE, kfuncs, struct_ops, arenas, exceptions) using only upstream rustc and LLVM.
And this is where the two communities started working together. Alexei Starovoitov, eBPF’s creator, wrote the rust-bpf PoC. Michal Rostecki, a rustc BPF target maintainer and a long-time Aya and bpf-linker contributor, is adding the CO-RE builtins to rustc; the exception-handling work came into rust-bpf from Yonghong Song, who works on LLVM’s BPF backend. Most of what’s still missing is shared ground: backend fixes that help C as much as Rust, rustc features every loader can use, and kernel pieces like the kfuncs these programs call.

