| Crates.io | elf-assembler |
| lib.rs | elf-assembler |
| version | 0.0.0 |
| created_at | 2025-10-16 11:14:07.069261+00 |
| updated_at | 2025-10-16 11:14:07.069261+00 |
| description | ELF assembler for Gaia project |
| homepage | |
| repository | https://github.com/nyar-vm/project-gaia |
| max_upload_size | |
| id | 1885768 |
| size | 89,092 |
一个用 Rust 编写的 ELF(可执行和链接格式)汇编器,可以生成 Linux 可执行文件。 该工具提供简单易用的 API 来创建基本的 ELF 可执行文件。
在您的 Cargo.toml 中添加此库:
[dependencies]
elf-assembler = { path = "../elf-assembler" }
gaia-types = { path = "../gaia-types" }
use elf_assembler::generator::{easy_hello_world, easy_exit_code, easy_console_log};
use gaia_types::helpers::Architecture;
use std::fs;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 生成 Hello World ELF 文件
let hello_elf = easy_hello_world(Architecture::X86_64)?;
fs::write("hello_world", &hello_elf)?;
// 生成退出代码 ELF 文件
let exit_elf = easy_exit_code(Architecture::X86_64, 42)?;
fs::write("exit_program", &exit_elf)?;
// 生成控制台输出 ELF 文件
let console_elf = easy_console_log(Architecture::X86_64, "Hello from ELF!")?;
fs::write("console_program", &console_elf)?;
println!("ELF 文件生成完成!");
Ok(())
}
use elf_assembler::writer::ElfBuilder;
fn main() {
let mut builder = ElfBuilder::new();
// 添加代码段
let code = vec![
0x48, 0xc7, 0xc0, 0x3c, 0x00, 0x00, 0x00, // mov rax, 60 (sys_exit)
0x48, 0xc7, 0xc7, 0x00, 0x00, 0x00, 0x00, // mov rdi, 0
0x0f, 0x05, // syscall
];
builder.add_code_section(code);
// 构建 ELF 文件
let elf_data = builder.build();
// 保存到文件
std::fs::write("custom_program", &elf_data).unwrap();
}
easy_hello_world(arch: Architecture) -> Result<Vec<u8>, GaiaError>
easy_exit_code(arch: Architecture, exit_code: u8) -> Result<Vec<u8>, GaiaError>
easy_console_log(arch: Architecture, message: &str) -> Result<Vec<u8>, GaiaError>
ElfBuilder 类提供了更灵活的 ELF 文件构建方式:
new() -> ElfBuilder - 创建新的构建器add_code_section(code: Vec<u8>) -> &mut Self - 添加代码段add_data_section(data: Vec<u8>) -> &mut Self - 添加数据段build() -> Vec<u8> - 构建最终的 ELF 文件目前支持以下架构:
生成的 ELF 文件包含:
运行测试套件:
cargo test
测试包括:
查看 examples/ 目录中的示例程序,了解如何使用此库。
此项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。
欢迎贡献!请随时提交 Pull Request 或创建 Issue。
生成的 ELF 文件遵循标准的 ELF 64 位格式:
\x7fELF生成的程序使用 Linux 系统调用:
sys_write (1): 输出到控制台sys_exit (60): 程序退出权限错误: 确保生成的文件有执行权限
chmod +x your_program
架构不支持: 目前只支持 x86-64 架构
Linux 专用: 生成的 ELF 文件只能在 Linux 系统上运行
使用以下工具调试生成的 ELF 文件:
readelf -h file - 查看 ELF 头信息objdump -d file - 反汇编代码hexdump -C file - 查看十六进制内容