diff --git a/Cargo.lock b/Cargo.lock index 3554f96..3921868 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,42 @@ version = 4 [[package]] name = "bdb" version = "0.0.0" +dependencies = [ + "nix", +] + +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "libc" +version = "0.2.172" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" + +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] diff --git a/Cargo.toml b/Cargo.toml index fbed181..56711d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ edition = "2024" publish = false [dependencies] +nix = { version = "0.29.0", features = ["process", "ptrace"] } diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..509006c --- /dev/null +++ b/build.rs @@ -0,0 +1,14 @@ +use std::process::Command; + +fn main() { + let test_prog_exe_path = &format!("{}/prog", std::env::var("OUT_DIR").unwrap()); + let test_prog_obj_path = &format!("{}.o", test_prog_exe_path); + Command::new("nasm") + .args(&["-f", "elf64", "src/prog.nasm", "-o", test_prog_obj_path]) + .spawn().expect("nasm build failed"); + Command::new("ld") + .args(&[test_prog_obj_path, "-o", test_prog_exe_path]) + .spawn().expect("linking failed"); + println!("cargo:rerun-if-changed=src/prog.nasm"); + println!("cargo:rustc-env=TEST_PROG_PATH={}", test_prog_exe_path); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e7a11a9..0687f85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,9 @@ +use nix::unistd::execv; +use std::ffi::CString; + fn main() { - println!("Hello, world!"); + println!("I am executing {}!", env!("TEST_PROG_PATH")); + + let path = CString::new(env!("TEST_PROG_PATH")).unwrap(); + execv(&path, &[&path]).unwrap(); } diff --git a/src/prog.nasm b/src/prog.nasm new file mode 100644 index 0000000..32fe1b0 --- /dev/null +++ b/src/prog.nasm @@ -0,0 +1,16 @@ +global _start +section .text +_start: + mov rax,1 + mov rdi, 1 + mov rsi, msg + mov rdx, msglen + syscall + + mov rdi,0 + mov rax,60 + syscall + +section .data + msg: db `Hello world\n` + msglen equ $-msg \ No newline at end of file