Search code examples
rustoperating-systemkernelbootloaderrust-cargo

How to compile a Rust kernel with an Assembly (NASM) bootloader


I have a simple 2-stage bootloader written in NASM, and I want to continue the OS kernel using Rust.

So I created a nightly Rust project with Cargo, and disabled std in the src/main.rs file. Now I am trying to link the Assembly files with the Cargo project, but without any success.

How should I compile and link the NASM bootloader with the Rust kernel?


Solution

  • After a couple of hours I compiled the code.

    The solution was (like Michael Petch suggested), to compile the assembly code into static .o files, then compile the rust code using xargo and a custom target. I compiled the Rust code as a static library, not a binary, so the output was a .a object file, and not an executable.

    Then I used gcc with a linker script to link together the object files and output the result as an ELF file. After that I used objcopy to with the -O binary flag to copy the ELF file to a .bin file. And finally I used dd to create an image file, that I could boot from.