| Crates.io | libloong |
| lib.rs | libloong |
| version | 0.7.0 |
| created_at | 2025-12-18 12:57:01.188575+00 |
| updated_at | 2025-12-19 19:49:20.938441+00 |
| description | Rust bindings for the libloong LoongArch emulator - a high-performance 64-bit LoongArch virtual machine |
| homepage | |
| repository | https://github.com/libriscv/libloong |
| max_upload_size | |
| id | 1992350 |
| size | 904,316 |
Rust bindings for the libloong LoongArch emulator.
Add to your Cargo.toml:
[dependencies]
libloong = "0.6"
use libloong::Machine;
use std::fs;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let binary = fs::read("program.elf")?;
let mut machine = Machine::new(&binary, Default::default())?;
Machine::setup_linux_syscalls();
machine.setup_linux(&["program"], &[])?;
machine.simulate(u64::MAX)?;
println!("Exit code: {}", machine.return_value());
Ok(())
}
// Call by name and get integer return value
machine.vmcall("factorial", &[5])?;
let result = machine.return_value();
println!("factorial(5) = {}", result);
// Call by address
let func_addr = machine.address_of("factorial");
machine.vmcall(func_addr, &[5])?;
let result = machine.return_value();
// For functions returning floats:
machine.vmcall("sin_approx", &[])?;
let result_f32 = machine.return_value_f32();
let result_f64 = machine.return_value_f64();
Requirements:
cargo build --release
The build script automatically builds the C++ library using CMake.
Run the examples (test programs are located in ../tests/programs/):
cargo run --example hello_world -- ../tests/programs/hello_world.elf
cargo run --example vmcall -- ../tests/programs/factorial.elf factorial 5
loongarch64-linux-gnu-gcc -O2 -static \
-Wl,-Ttext-segment=0x200000 \
-o program.elf program.c
The -Wl,-Ttext-segment=0x200000 flag is required for the flat memory model.
binary-translation (enabled by default)Enables JIT compilation for better performance. This feature requires network access during build phase (although only once).
To disable:
[dependencies]
libloong = { version = "0.6", default-features = false }