Executing a target assembly program with execv

This commit is contained in:
Elnath 2025-04-18 17:24:17 +02:00
parent d14122fe03
commit 46d173bd56
5 changed files with 77 additions and 1 deletions

39
Cargo.lock generated
View File

@ -5,3 +5,42 @@ version = 4
[[package]] [[package]]
name = "bdb" name = "bdb"
version = "0.0.0" 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",
]

View File

@ -5,3 +5,4 @@ edition = "2024"
publish = false publish = false
[dependencies] [dependencies]
nix = { version = "0.29.0", features = ["process", "ptrace"] }

14
build.rs Normal file
View File

@ -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);
}

View File

@ -1,3 +1,9 @@
use nix::unistd::execv;
use std::ffi::CString;
fn main() { 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();
} }

16
src/prog.nasm Normal file
View File

@ -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