orecc-elf 0.0.2

Easy read/write ELF 32/64 relocatibles/executables/dynamics
Documentation
use super::*;

#[test]
fn x86() {
    let data = [
        0xB8, 0x01, 0x00, 0x00, 0x00, 0xBB, 0x00, 0x00, 0x00, 0x00, 0xCD, 0x80,
    ];
    let elf = ELF::<u32>::new(
        Ident::new(Class::ELF32, ByteOrder::LSB, ABI::None, 0),
        Type::Exec,
        Machine::X86,
        true,
        vec![SegmentTemplate::new(
            SegmentType::Load,
            data.to_vec(),
            data.len() as _,
            SegmentFlags::Readable as u32 | SegmentFlags::Executable as u32,
        )],
        Vec::new(),
    )
    .unwrap();
    let mut buffer = std::io::Cursor::new(Vec::new());
    elf.write(&mut buffer).unwrap();

    // Source: https://dacvs.neocities.org/1exit
    let expectation = [
        0x7F, 0x45, 0x4C, 0x46, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x54, 0x80, 0x04, 0x08, 0x34, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x20, 0x00, 0x01,
        0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00,
        0x54, 0x80, 0x04, 0x08, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00,
        0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0xB8, 0x01, 0x00, 0x00, 0x00, 0xBB,
        0x00, 0x00, 0x00, 0x00, 0xCD, 0x80,
    ];
    assert_eq!(&buffer.into_inner(), &expectation);
}

#[test]
fn x86_64() {
    let data = [
        0x48, 0xC7, 0xC0, 0x3C, 0x00, 0x00, 0x00, 0x48, 0xC7, 0xC7, 0x2A, 0x00, 0x00, 0x00, 0x0F,
        0x05,
    ];
    let elf = ELF::<u64>::new(
        Ident::default(),
        Type::Exec,
        Machine::X86_64,
        true,
        vec![SegmentTemplate::new(
            SegmentType::Load,
            data.to_vec(),
            data.len() as _,
            SegmentFlags::Readable as u32 | SegmentFlags::Executable as u32,
        )],
        Vec::new(),
    )
    .unwrap();
    let mut buffer = std::io::Cursor::new(Vec::new());
    elf.write(&mut buffer).unwrap();

    // Source: https://www.youtube.com/watch?v=XH6jDiKxod8 (first comment)
    let expectation = [
        0x7F, 0x45, 0x4C, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x02, 0x00, 0x3E, 0x00, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, 0x40, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00, 0x01, 0x00, 0x40, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x48, 0xC7, 0xC0, 0x3C, 0x00, 0x00, 0x00, 0x48, 0xC7, 0xC7, 0x2A, 0x00, 0x00, 0x00, 0x0F,
        0x05,
    ];
    assert_eq!(&buffer.into_inner(), &expectation);
}