Open jyao1 opened 2 years ago
Our target x86_64-unknown-uefi
doesn't support std
library.So we must use #![no_std]
to build for that target. Here is platform-support list.
the following questions is for those not support std target.
A. No panic_handler B. No heap alloc C. A lot of crates in crate.io do not support no_std (eg. rustls).
A and B easy to solve.
For A we can simply add #[panic_handler]
For B we can reuse alloc crate and provide a #[global_allocator]
and a alloc_error_handler
. This also need enable unstable features #![feature(alloc_error_handler)]
But for C eh... Modifying the crate's source code might be work #![no_std]
. (use prelude or add support no_std). But there is a lot of maintenance work and upstream work.
So we need a way to add std support for C.
Here are discussions in the rust community https://github.com/rust-lang/wg-cargo-std-aware
One way (case 2 or 3) is use build-std
feature. see: https://github.com/rust-lang/wg-cargo-std-aware#mvp-implementation
Another way (case 1) is add modify rust source code and add support for x86_64-unknown-uefi target
rustls (unmodified) build with host target, into host exe -- success basic feature that we need guarantee.
rustls (unmodified) build with modified rust (add uefi support for library/std), build with cargo-build -- success build modified rust https://github.com/xiaoyuxlu/rust/tree/b/uefi_std and build payload(https://github.com/jyao1/rust-td/tree/master/rust-rustls-payload) with unmodified rustls
./x.py build -i
./x.py install -i
./x.py install -i --target x86_64-unknown-uefi library/std --stage 2 -v
## change dir to rust-rustls-payload
cargo build --target x86_64-unknown-uefi --release
rustls ( + std::prelude): build with cargo-xbuild / cargo build -Zbuild-std -- success ref: https://github.com/jyao1/rust-uefi-std-stub ref: https://github.com/jyao1/rustls/tree/uefi_support
rustls (unmodified): build with __CARGO_TESTS_ONLY_SRC_ROOT=<modified rust source code tree path> cargo build -Z build-std
-- success
ref: https://github.com/jyao1/rust-td/tree/master/rust-rustls-payload
cargo build -Zbuild-std=core,std,compiler_builtins,alloc,panic_abort -Zbuild-std-features=compiler-builtins-mem,panic_immediate_abort --target x86_64-unknown-uefi --release
If we use rust_std (in 1 and 3), we need resolve problem (similar to no_std in 2): A. rust_std panic for UEFI B. rust_std alloc for UEFI
Question for 1 and 3:
A: Payload can register a customer panic_handler by using std::panic::set_hook
.
B: Payload can use #[global_allocator]
to specify a allocator.
Case | Rustls modification | Rust modification | Rebuild rust | Target | Build method | Status(rustls-payload with std) (if use #![no_std] NO else YES ) |
---|---|---|---|---|---|---|
0 | NO | NO | NO | host | cargo build | YES |
1 | NO | YES | YES | x86_64-unknown-uefi | cargo build | YES |
2 | YES with std_stub | NO | NO | x86_64-unknown-uefi | cargo build -Zbuild-std | NO |
3 | NO | YES | NO | x86_64-unknown-uefi | __CARGO_TESTS_ONLY_SRC_ROOT=[path-to-rust-src]/rust cargo build -Zbuild-std | YES |
sgx need add extern sgx_tstd as std
in bottom of crate. and add use std::prelude::v1::*
need maintain another crate branch for sgx
use target.json to indicate a std lib ?
REF: https://zhuanlan.zhihu.com/p/406785047