Compiling FreeBSD kernel module directly with Rust without invoking C and FreeBSD build system.
In the previous article, the Rust crates: Kernel Module Interface and Kernel Programming Interface were introduced and it was described how to use it. Also it was promised to find out the way to compile the project without invoking the FreeBSD build system. So, this is it, but with some drawbacks like it still requires to invoke the objcopy manually and separately.
The template for the module is located there freebsd-kmod-native-template.
Current stage of Cargo development allows to achieve this goal. To do so, the cargo should be instructed with the following: which linker should be called, which ABIs should be prohibited, what is the target and a linker script.
In the “x86_64-unknown-freebsd-kernel.json” the following changes were made:
领英推荐
A “cargo build –release” should be invoked and a module ELF binaries will be generated at “$projectroot/target/x86_64-unknown-freebsd-kernel/release/”. This is a non stripped “ko” file.
The next step is to call a “post_build.sh” which is provided at the project root. This is a script which performs manipulations on the binary file which includes:
The argument of the script is a name of the binary (usually a title of the project which is provided in crate). i.e "./post_build rust-kpi-test"
Unfortunately, cargo does not provide a post-build functionality, so you need to call everything manually and in two steps.
The “post_clean.sh” is a housekeeping helper which is followed after “cargo clean”. It removes the “kmod” directory.