add unsafe examples + update README

This commit is contained in:
ahgamut 2022-06-26 20:23:21 +05:30
parent 3a4936d455
commit 2561dec542
2 changed files with 29 additions and 7 deletions

View file

@ -13,9 +13,6 @@ use `rustc` or equivalent compiler to generate `.o` files, and then write a
shell script that does the linking with the expected flags. I have not tried
this method.
> a previous attempt at a Rust APE was done without using the `std` crate. You
can view that in the [`without-std` branch of this repo][without-std-branch].
## Building a Rust APE with the `std` crate
1. Download the Cosmopolitan Libc [amalgamation][amalg-download] into the `libcosmo` folder:
@ -52,7 +49,7 @@ For reference, this worked when I tried it for `nightly-x86_64-linux-gnu` and:
is required.
```bash
cargo +nightly build -Zbuild-std=panic_abort,std -Zbuild-std-features="" --target=./x86_64-unknown-linux-cosmo.json
cargo +nightly build -Zbuild-std=libc,panic_abort,std -Zbuild-std-features="" --target=./x86_64-unknown-linux-cosmo.json
```
For reference, I used the below versions of `gcc` and `ld.bfd`
@ -81,11 +78,13 @@ objcopy -SO binary ./target/x86_64-unknown-linux-cosmo/debug/hello_world.com.dbg
./hello_world.com
# see syscalls made by the APE
./hello_world.com --strace
# ls ./target/x86_64-unknown-linux-cosmo/debug/*.com.dbg
```
Now we have an Actually Portable Executable built with Rust! There might some
edge cases that I haven't noticed, so clone/fork the repo and try it out! The
[`without-std` branch][without-std-branch] of this repo might be useful.
Now we have Actually Portable Executables built with Rust! I also built a few
more executables using the code from [Rust By Example][rbe], and an APE that
doesn't use the `std` crate. There might some edge cases that I haven't noticed,
so clone/fork the repo and try it out!
## TODOs
@ -108,6 +107,7 @@ build command.
[without-std-branch]: https://github.com/ahgamut/rust-ape-example/tree/without-std
[rust]: https://rust-lang.org
[rbe]: https://doc.rust-lang.org/rust-by-example/
[cosmo]: https://github.com/jart/cosmopolitan
[cosmo-nightly]: https://github.com/jart/cosmopolitan/commit/893cc06fc2ca7f84bc2238566f29d10d32999725
[amalg-download]: https://justine.lol/cosmopolitan/download.html

22
src/bin/printf-nostd.rs Normal file
View file

@ -0,0 +1,22 @@
#![no_main]
#![no_std]
#![feature(rustc_private)]
extern crate libc;
#[no_mangle]
pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize {
// Since we are passing a C string the final null character is mandatory
const HELLO: &'static str = "Hello, world! %d + %d = %d\n\0";
let x: i32 = 1;
let y: i32 = 2;
unsafe {
libc::printf(HELLO.as_ptr() as *const _, x, y, x+y);
}
0
}
#[panic_handler]
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}