Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Rust代码编译为裸机32位x86(i686)代码?我应该使用什么编译目标?_Rust - Fatal编程技术网

如何将Rust代码编译为裸机32位x86(i686)代码?我应该使用什么编译目标?

如何将Rust代码编译为裸机32位x86(i686)代码?我应该使用什么编译目标?,rust,Rust,我想使用cargo/Rust为x86编译裸机32位代码(在32位模式下又称i686又称x86_64)。我需要什么目标?在中,我找不到任何实用的东西。不幸的是,Rust编译器没有内置的目标定义[Rust nightly 1.542021年6月],但您可以提供自定义目标定义: /x86-unknown-bare_metal.json { "llvm-target": "i686-unknown-none", "data-layout"

我想使用cargo/Rust为x86编译裸机32位代码(在32位模式下又称i686又称x86_64)。我需要什么目标?在中,我找不到任何实用的东西。

不幸的是,Rust编译器没有内置的目标定义[Rust nightly 1.542021年6月],但您可以提供自定义目标定义:

/x86-unknown-bare_metal.json

{
  "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位保护模式时)。我不是100%确定“功能”,因为它们可能取决于你想做什么/需要什么<代码>i686指32位模式下的x86_64和

此外,还应添加该文件

/.cargo/config.toml

[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"
# 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" ]
现在,您可以使用
cargo build
使用32位x86代码构建裸机二进制文件

// 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 doesn't output anything
/// to the object file. I don't know why it is like this.
fn _start() -> ! {
    loop {}
}

#[inline(never)]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {
        atomic::compiler_fence(Ordering::SeqCst);
    }
}
为方便起见,您还应添加

/rust toolchain.toml

[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"
# 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" ]