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 andSelf::write()
to write it to a fileT
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
- ABI. There is planety to chose from, but you probably should just use ABI::None
- Byte order, stored in e_ident.
- Class, stored in e_ident. ELF32/ELF64
- A custom error type
- Machine. There is A LOT of them. Most common are Machine::X86, Machine::X86_64, Machine::ARM and Machine::ARM64
- p_flags. Essentially permissions. See [
Segment::flags
] for details - p_type. A segment type (type of a program header). Most segments will probably have type SegmentType::Load
- Type of the ELF.
Traits
Type Aliases
- A custom result type