I came with a minimal template to run in Linux-compatible OS with a x86_64 architecture.
This template consists of barely the same function in different locations.
The function is a write syscall invocation to standard output of an &str.
fn _print(msg: &str) {
let bytes = msg.as_bytes();
unsafe {
core::arch::asm!(
"syscall",
inlateout("rax") 1usize => _,
in("rdi") 1usize,
in("rsi") bytes.as_ptr(),
in("rdx") bytes.len(),
out("rcx") _,
out("r11") _,
options(nostack)
);
}
}
There are several flavors (locations) of this same function:
./src/main.rs
./src/amod.rs
./src/lib.rs
./crates/print/src/lib.rs
Where src/main.rs is bounded to a binary described in Cargo.toml; src/amod.rs codes a module declared in src/main.rs.
[[bin]]
name = "template"
path = "src/main.rs"
test = false
; the src/lib.rs and crates/print/src/lib.rs codes a libraries also defined in Cargo.toml.
[workspace]
members = ["crates/print"]
[dependencies]
print = { path = "crates/print" }
[lib]
name = "template"
path = "src/lib.rs"
test = false
The Cargo.toml overrides the default lto
in release mode to true;
[profile.release]
lto = true
Then by running:
git clone git@github.com:ze-gois/rust_template_x86_64
cd rust_template_x86_64
cargo run --release
works fine,
❯ cargo run --release
Finished `release` profile [optimized] target(s) in 0.00s
Running `target/x86_64-unknown-none/release/template`
Test 0: src/main.rs
Test 1: src/amod.rs
Test 2: src/lib.rs
Test 3: crates/print/src/lib.rs
Test 4: crates/print/src/lib.rs (static)
0, 1, 2, 3, 4.....
however the
cargo run
does not work.
➜ rust_template_x86_64 git:(main) cargo run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
Running `target/x86_64-unknown-none/debug/template`
Test 0: src/main.rs
Test 1: src/amod.rs
[1] 4409 segmentation fault (core dumped) cargo run