环境
- Time 2022-11-13
- WSL-Ubuntu 22.04
- QEMU 6.2.0
- Rust 1.67.0-nightly
前言
说明
参考:https://os.phil-opp.com/minimal-rust-kernel
目标
编译个 x64 平台的独立可执行程序。
切换到 nightly 版本
项目目录下新建 rust-toolchain 文件,文件内容为:nightly。
main.rs
#![no_std]
#![no_main]
#[no_mangle]
pub extern "C" fn _start() -> ! {
loop {}
}
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
Cargo.toml
[package]
edition = "2021"
name = "mos"
version = "0.1.0"
[dependencies]
mos.json
编译的目标文件,定义了平台等一些信息,放到项目根目录下。
{
"llvm-target": "x86_64-unknown-none",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"arch": "x86_64",
"target-endian": "little",
"target-pointer-width": "64",
"target-c-int-width": "32",
"os": "none",
"executables": true,
"linker-flavor": "ld.lld",
"linker": "rust-lld",
"panic-strategy": "abort",
"disable-redzone": true,
"features": "-mmx,-sse,+soft-float"
}
config.toml
[unstable]
build-std-features = ["compiler-builtins-mem"]
build-std = ["core", "compiler_builtins"]
[build]
target = "mos.json"
文件目录结构
├── Cargo.lock
├── Cargo.toml
├── gdb.sh
├── mos.json
├── qemu.sh
├── rust-toolchain
├── src
│ └── main.rs
└── .cargo
└── config.toml
总结
使用 Rust 编写了一个在 x64 平台上的独立可执行程序。