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§

ELF
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
Ident
e_ident. Specifies class, byte order and ABI of the ELF
Section
Section, when linking turned into segment
Segment
A segment, this gets loaded into memory when elf file gets executed
SegmentTemplate
A segment template, Segment will be generated from it

Enums§

ABI
ABI. There is planety to chose from, but you probably should just use ABI::None
ByteOrder
Byte order, stored in e_ident.
Class
Class, stored in e_ident. ELF32/ELF64
Error
A custom error type
Machine
Machine. There is A LOT of them. Most common are Machine::X86, Machine::X86_64, Machine::ARM and Machine::ARM64
SegmentFlags
p_flags. Essentially permissions. See [Segment::flags] for details
SegmentType
p_type. A segment type (type of a program header). Most segments will probably have type SegmentType::Load
Type
Type of the ELF.

Traits§

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

Type Aliases§

Result
A custom result type