首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将锈蚀代码编译成裸金属32位x86 (i686)代码?我应该使用什么编译目标?

如何将锈蚀代码编译成裸金属32位x86 (i686)代码?我应该使用什么编译目标?
EN

Stack Overflow用户
提问于 2021-06-09 10:28:12
回答 1查看 1.6K关注 0票数 3

我想编译裸金属32位代码的x86 (又名i686,又名32位模式下的x86_64 )使用货/锈。我需要什么目标?在官方支持的目标列表中,我找不到任何东西,这是很实用的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-09 10:28:12

不幸的是,Rust编译器没有内置的目标定义Rust夜1.54 (2021年6月),但是您可以提供一个自定义目标定义:

/x86-unknown-bare_metal.json

代码语言:javascript
复制
{
  "llvm-target": "i686-unknown-none",
  "data-layout": "e-m:e-i32:32-f80:128-n8:16:32-S128-p:32:32",
  "arch": "x86",
  "target-endian": "little",
  "target-pointer-width": "32",
  "target-c-int-width": "32",
  "os": "none",
  "executables": true,
  "linker-flavor": "ld.lld",
  "linker": "rust-lld",
  "panic-strategy": "abort",
  "disable-redzone": true,
  "features": "+soft-float,+sse"
}

此定义将指针宽度设置为32,用于编译代码,这些代码可用于早期x86引导过程(当您已经处于32位保护模式时)。我对“特性”不是百分之百肯定,因为它们可能取决于您想做什么/需要什么。i686指32位模式下的x86_64和数据布局在这里解释。.

此外,还应该添加文件。

/.cargo/config.toml

代码语言:javascript
复制
[unstable]
# cross compile core library for custom target
build-std = ["core", "compiler_builtins"]
build-std-features = ["compiler-builtins-mem"]

[build]
# points to file in project root
target = "x86-unknown-bare_metal.json"

现在,您可以用32位x86代码使用cargo build构建一个裸金属二进制文件。

代码语言:javascript
复制
// disable rust standard library
#![no_std]
// disables Rust runtime init,
#![no_main]

// see https://docs.rust-embedded.org/embedonomicon/smallest-no-std.html
#![feature(lang_items)]

// see https://docs.rust-embedded.org/embedonomicon/smallest-no-std.html
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

use core::panic::PanicInfo;
use core::sync::atomic;
use core::sync::atomic::Ordering;

#[no_mangle]
/// The name **must be** `_start`, otherwise the compiler throws away all code as unused. 
/// The name can be changed by passing a different entry symbol as linker argument.
fn _start() -> ! {
    loop {}
}

#[inline(never)]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {
        atomic::compiler_fence(Ordering::SeqCst);
    }
}

为了方便起见,您还应该添加

/rust-toolchain.toml

代码语言:javascript
复制
# With this file, another toolchain to the currently selected one will be used, when you execute `cargo build`.
# https://rust-lang.github.io/rustup/overrides.html

[toolchain]
# equals to rust nightly 1.54 as of the release day 2021-05-10
channel = "nightly-2021-05-10"
components = [ "rust-src", "rust-std", "rustc", "cargo" ]
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67902309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档