0183-x64 平台独立程序

环境

  • 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 平台上的独立可执行程序。

附录

posted @   jiangbo4444  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-07-15 【JavaScript】事件介绍
点击右上角即可分享
微信分享提示