#elf #elf-file #object #api

build orecc-elf

Easy read/write ELF 32/64 relocatibles/executables/dynamics

2 releases

0.0.2 Oct 3, 2023
0.0.1 Oct 1, 2023

#662 in Build Utils

MIT license

40KB
845 lines

Contains (ELF exe/lib, 1KB) test.o

Easy read/write ELF 32/64 relocatibles/executables/dynamics

This crate is designed to use with orecc-back. However it's completely standalone. It doesn't even have dependencies!

Work in progress: [x] Writing ELFs: segments [] Writing ELFs: sections [] Reading ELFs: segments [] Reading ELFs: sections


lib.rs:

Easy read/write ELF 32/64 relocatibles/executables/dynamics

To read an elf file:

let mut file = std::fs::File::open("test.o").unwrap();
dbg!(orecc_elf::ELF::<u64>::read(&mut file));

To write an elf file (source: https://www.youtube.com/watch?v=XH6jDiKxod8):

let mut file = std::fs::File::create("test.o").unwrap();
// An x86 program that just exits
let data = [
    0xB8, 0x01, 0x00, 0x00, 0x00,
    0xBB, 0x00, 0x00, 0x00, 0x00,
    0xCD, 0x80,
];
orecc_elf::ELF::<u32>::new(
    orecc_elf::Ident::new(
        orecc_elf::Class::ELF32,
        orecc_elf::ByteOrder::LSB,
        orecc_elf::ABI::None,
        0,
    ),
    orecc_elf::Type::Exec,
    orecc_elf::Machine::X86,
    true,
    vec![
        orecc_elf::SegmentTemplate::new(
            orecc_elf::SegmentType::Load,
            data.to_vec(),
            data.len() as _,
            orecc_elf::SegmentFlags::Readable as u32 | orecc_elf::SegmentFlags::Executable as u32,
        )
    ],
    Vec::new(),
)
.unwrap()
.write(&mut file)
.unwrap();

x86_64 version:

let mut file = std::fs::File::create("test.o").unwrap();
// An x86_64 program that just exits
let data = [
    0x48, 0xC7, 0xC0, 0x3C, 0x00, 0x00, 0x00,
    0x48, 0xC7, 0xC7, 0x2A, 0x00, 0x00, 0x00,
    0x0F, 0x05,
];
orecc_elf::ELF::<u64>::new(
    orecc_elf::Ident::default(),
    orecc_elf::Type::Exec,
    orecc_elf::Machine::X86_64,
    true,
    vec![
        orecc_elf::SegmentTemplate::new(
            orecc_elf::SegmentType::Load,
            data.to_vec(),
            data.len() as _,
            orecc_elf::SegmentFlags::Readable as u32 | orecc_elf::SegmentFlags::Executable as u32,
        )
    ],
    Vec::new(),
)
.unwrap()
.write(&mut file)
.unwrap();

Dependencies

~25KB