# Compiling a Rust Kernel Module



## pcoppock1 (Mar 21, 2020)

As part of an undergraduate project at the Georgia Institute of Technology I am attempting to compile a basic kernel module in Rust. I found this GitHub repository: https://github.com/johalun/echo, but the code is three years old, and I ran into an error I don't know how to fix.

Since then, I've written the C kernel module example at https://medium.com/rossdotpink/writing-a-simple-freebsd-kernel-module-9302bd4cfae1. I've also experimented with linking a Rust static library into a C binary and perused the FreeBSD kernel module Makefiles.

I'm not familiar enough with make(1) to understand the kernel module Makefiles, but my guess is that it's possible to link a compiled Rust object into a C kernel module wrapper.

Does anyone have some suggestions as to what steps I should take going forward?


----------



## Alain De Vos (Mar 21, 2020)

Poor documentation is a pita. There are books for old kernels.
You could try to find the exact kernel version for which you have found code , install that kernel and the code should run.
For the current kernel 12.1 I have totally no idea where to start except read the code, which seems to be a very steep learning curve.


----------



## pcoppock1 (Mar 22, 2020)

Alain De Vos, that's great guidance. I'll try to see what kernel version (as well as Rust version) the code ran for.


----------



## shkhln (Mar 22, 2020)

pcoppock1 said:


> and I ran into an error I don't know how to fix



What error?


----------



## pcoppock1 (Mar 23, 2020)

shkhln, here is a link to the GitHub issue: https://github.com/johalun/echo/issues/1#issue-581290721. It appears to be a Rust issue. Rust has probably changed significantly since the code was written.


----------



## shkhln (Mar 23, 2020)

> Error loading target specification: Field _target-c-int-width_ in target specification is required



There is _x86_64-kernel-freebsd.json_ in the project root and another one in the _crates/freebsd_kernel_std_ directory.


----------



## pcoppock1 (Mar 31, 2020)

I got the target spec. errors to disappear. Now I have a bunch of Rust errors. Here's my GitHub fork in case anyone is interested.


----------

