mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-11-21 23:41:00 +00:00
checked with latest nightly
This commit is contained in:
parent
298b7453c2
commit
069ad60349
2 changed files with 8 additions and 29 deletions
10
README.md
10
README.md
|
@ -1,7 +1,10 @@
|
||||||
# Actually Portable Executables with Cosmopolitan Libc and Rust
|
# Actually Portable Executables with Cosmopolitan Libc and Rust
|
||||||
|
|
||||||
This repository contains a simple `Hello world!` example in the [Rust][rust]
|
This repository contains a simple `Hello world!` example in the [Rust][rust]
|
||||||
programming language, that builds with [Cosmopolitan Libc][cosmo].
|
programming language, that builds with [Cosmopolitan Libc][cosmo]. To build it
|
||||||
|
you need a recent version of `gcc` (9 or 10 ought to be good), a recent version
|
||||||
|
of `binutils` (`ld.bfd` and `objcopy`), and `bash` because I wrote a simple
|
||||||
|
filter script.
|
||||||
|
|
||||||
I created a [custom compilation target][custom-target] for Rust, called
|
I created a [custom compilation target][custom-target] for Rust, called
|
||||||
`x86_64-unknown-linux-cosmo`, to provide a build process that uses the
|
`x86_64-unknown-linux-cosmo`, to provide a build process that uses the
|
||||||
|
@ -43,6 +46,8 @@ For reference, this worked when I tried it for `nightly-x86_64-linux-gnu` and:
|
||||||
* the Rust binaries on June 22 2022 (5750a6aa2 2022-06-20)
|
* the Rust binaries on June 22 2022 (5750a6aa2 2022-06-20)
|
||||||
* the Rust binaries on June 25 2022 (fdca237d5 2022-06-24)
|
* the Rust binaries on June 25 2022 (fdca237d5 2022-06-24)
|
||||||
* the Rust binaries on June 26 2022 (20a6f3a8a 2022-06-25)
|
* the Rust binaries on June 26 2022 (20a6f3a8a 2022-06-25)
|
||||||
|
* the Rust binaries on June 30 2022 (ddcbba036 2022-06-29)
|
||||||
|
* the Rust binaries on July 27 2022 (4d6d601c8 2022-07-26)
|
||||||
|
|
||||||
3. run `cargo build` to get the debug executable. This uses a bash script that
|
3. run `cargo build` to get the debug executable. This uses a bash script that
|
||||||
removes unnecessary linker arguments. A recent version of `gcc` and `ld.bfd`
|
removes unnecessary linker arguments. A recent version of `gcc` and `ld.bfd`
|
||||||
|
@ -72,13 +77,14 @@ This program has absolutely no warranty.
|
||||||
4. run `objcopy` on the debug binary to obtain the APE:
|
4. run `objcopy` on the debug binary to obtain the APE:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
# look at the built debug binaries
|
||||||
|
ls ./target/x86_64-unknown-linux-cosmo/debug/*.com.dbg
|
||||||
# objcopy is the same version as ld.bfd above
|
# objcopy is the same version as ld.bfd above
|
||||||
objcopy -SO binary ./target/x86_64-unknown-linux-cosmo/debug/hello_world.com.dbg ./hello_world.com
|
objcopy -SO binary ./target/x86_64-unknown-linux-cosmo/debug/hello_world.com.dbg ./hello_world.com
|
||||||
# run the APE
|
# run the APE
|
||||||
./hello_world.com
|
./hello_world.com
|
||||||
# see syscalls made by the APE
|
# see syscalls made by the APE
|
||||||
./hello_world.com --strace
|
./hello_world.com --strace
|
||||||
# ls ./target/x86_64-unknown-linux-cosmo/debug/*.com.dbg
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Now we have Actually Portable Executables built with Rust! I also built a few
|
Now we have Actually Portable Executables built with Rust! I also built a few
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
#![no_main]
|
|
||||||
#![no_std]
|
|
||||||
#![feature(rustc_private)]
|
|
||||||
|
|
||||||
extern crate libc;
|
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
fn ShowCrashReports();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize {
|
|
||||||
unsafe {
|
|
||||||
ShowCrashReports();
|
|
||||||
let mut buffer = libc::malloc(13);
|
|
||||||
const c_str: &'static str = "Hello\0";
|
|
||||||
libc::strcpy(buffer as *mut i8, c_str.as_ptr() as *const i8);
|
|
||||||
libc::free(buffer);
|
|
||||||
libc::printf(buffer as *const _);
|
|
||||||
}
|
|
||||||
0
|
|
||||||
}
|
|
||||||
|
|
||||||
#[panic_handler]
|
|
||||||
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
|
|
||||||
loop {}
|
|
||||||
}
|
|
Loading…
Reference in a new issue