Raspberry Pi Bare Bones Rust: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
m forgot to add link
No edit summary
 
(One intermediate revision by one other user not shown)
Line 1:
{{FirstPerson}}
{{You}}
{{BeginnersWarning}}
{{Rating|3}}
Line 15 ⟶ 16:
In the C tutorial, you created a reference to a symbol "kernel_main" in boot.S.
 
<sourcesyntaxhighlight lang="asm">
// Call kernel_main
ldr r3, =kernel_main
</syntaxhighlight>
</source>
 
At the end of that tutorial, the compiled kernel.c object file kernel.o provided a definition for this symbol using its function "kernel_main" to the linker when creating kernel.elf.
Line 28 ⟶ 29:
For brevity we will assume our environment has no Rust installation.
 
<sourcesyntaxhighlight lang="bash"> curl https://sh.rustup.rs -sSf | sh </sourcesyntaxhighlight>
 
This installs rustup, which will manage the Rust compiler and Cargo.
 
<sourcesyntaxhighlight lang="bash"> rustup component add rust-src </sourcesyntaxhighlight>
 
We will need to recompile some of the Rust language later, so we need this component.
 
<sourcesyntaxhighlight lang="bash"> cargo install xargo </sourcesyntaxhighlight>
 
We will use [https://github.com/japaric/xargo xargo] to build our project, which works just like a drop-in replacement for Cargo. xargo automatically handles the significant task of compiling and linking in components of the Rust language we will need (such as libcore.rs) for our target.
 
<sourcesyntaxhighlight lang="bash">
xargo new kernel
cd kernel
rustup override set nightly
</syntaxhighlight>
</source>
 
This creates our crate and tells rustup to use the nightly Rust toolchain for it. We will need features of Rust that are not available in stable.
Line 50 ⟶ 51:
In this directory create a file called arm-none-eabihf.json with this content:
 
<sourcesyntaxhighlight lang="python">
{
"llvm-target": "arm-none-eabihf",
Line 68 ⟶ 69:
"no-compiler-rt": true
}
</syntaxhighlight>
</source>
 
Rust uses LLVM as a backend, which expects this kind of specification file for non-standard (has no triple) compilation targets. Filling out the values of this is beyond the scope of this tutorial; more information can be found in [https://github.com/japaric/rust-cross rust-cross] which covers cross compiling Rust in detail.
Line 90 ⟶ 91:
</pre>
 
We can build our rlib with a definition of kernel_main now: <sourcesyntaxhighlight lang="bash"> xargo build --target arm-none-eabihf</sourcesyntaxhighlight>.
 
Now, revisit the command which built kernel.elf in the C tutorial, but instead of kernel.o, provide the libkernel.rlib xargo just built in the target directory.
 
<sourcesyntaxhighlight lang="bash">arm-none-eabi-gcc -T linker.ld -o myos.elf -ffreestanding -O2 -nostdlib boot.o path/to/libkernel.rlib </sourcesyntaxhighlight>
 
This is it! You've built Rust code for bare metal arm that will run on the Raspberry Pi!
Line 183 ⟶ 184:
If you run this you will see your text printed out on the virtual serial port:
 
<sourcesyntaxhighlight lang="bash">
qemu-system-arm -M raspi2 -kernel myos.elf -serial stdio
</syntaxhighlight>
</source>
 
====External Resources====