Crate orecc_elf

source ·
Expand description

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();

Structs

  • The ELF file itself. This what you will be using. Use Self::read() to read if from a file, Self::new() to construct it from scratch and Self::write() to write it to a file T generic can be u32 for ELF32 and u64 for ELF64
  • e_ident. Specifies class, byte order and ABI of the ELF
  • Section, when linking turned into segment
  • A segment, this gets loaded into memory when elf file gets executed
  • A segment template, Segment will be generated from it

Enums

Traits

  • ELF is supposed to be 32/64. This is a trait to specify this. It’s implemented for u32 and u64.

Type Aliases