> module
std::re::elf
read and build ELF files
std::re::elf reads ELF files (Linux executables, shared objects and object files) and builds new shared objects.
ElfFile is a file as it is on disk. Its headers are fields: header, sections and program_headers. Its methods compute what the headers point at: symbols, relocations and the mapping between virtual addresses and file offsets.
ImageDef builds a shared object from scratch.
Section and segment flags are integers. The *_names functions turn them into flag names and the matching functions turn a list of names back into an integer. Section flags are "WRITE", "ALLOC", "EXECINSTR", "MERGE", "STRINGS", "INFO_LINK" and "TLS"; segment flags are "READ", "WRITE" and "EXECUTE".
Backed by pewter. For a PE or ELF file read by virtual address, see AnyImage in xur.std.re(7).
> functions
dynamic_proxy(class: ElfClass, machine: ElfMachine, soname: str, needed: list<str>) -> Result<bytes, str>Build a minimal shared object named soname whose only job is to make the loader load the libraries in needed (one DT_NEEDED each). Because ELF symbol lookup is flat, a program linked against it still finds the real library's symbols.
Combine section flag names, such as ["ALLOC",\"EXECINSTR"], into the integer for a section header. An unknown name gives Err.
The names of the section flags set in bits, for example in section.header.flags.
Combine segment flag names into the integer for a program header.
> types
| ElfClass::Elf32 | |
| ElfClass::Elf64 |
| ElfData::Little | |
| ElfData::Big |
| header | ElfHeader | |
| program_headers | list<ProgramHeader> | |
| sections | list<Section> | |
| file_bytes | bytes |
elf_file.is_64() -> boolWhether it is a 64-bit (ELFCLASS64) file.
The virtual address that file offset offset is loaded at, or None.
len bytes of file data at vaddr, or None if they aren't all inside one segment's file data (so .bss can't be read).
The pointers the loader writes when it loads the file at its preferred address, as (location,\value) virtual-address pairs sorted by location. Covers relative relocations and absolute relocations against dynamic symbols.
The relocations of every SHT_REL and SHT_RELA section.
The first section named name, with its header and data.
The symbols of .symtab and .dynsym. A symbol's dynamic field says which table it came from.
| class | ElfClass | |
| byte_order | ElfData | |
| os_abi | u8 | |
| abi_version | u8 | |
| file_type | ElfType | |
| machine | ElfMachine | |
| version | u32 | |
| entry | u64 | |
| program_header_offset | u64 | |
| section_header_offset | u64 | |
| flags | u32 | |
| header_size | u16 | |
| program_header_entry_size | u16 | |
| program_header_count | u16 | |
| section_header_entry_size | u16 | |
| section_header_count | u16 | |
| section_name_index | u16 | Index of the section holding section names. |
| ElfMachine::None | |
| ElfMachine::X86 | |
| ElfMachine::Arm | |
| ElfMachine::X86_64 | |
| ElfMachine::AArch64 | |
| ElfMachine::RiscV | |
| ElfMachine::Other(u16) |
| ElfType::None | |
| ElfType::Rel | Relocatable object file. |
| ElfType::Exec | Executable. |
| ElfType::Dyn | Shared object (also position independent executables). |
| ElfType::Core | |
| ElfType::Other(u16) |
An owned, writable ELF image.
| class | ElfClass | |
| byte_order | ElfData | |
| os_abi | u8 | |
| abi_version | u8 | |
| file_type | ElfType | |
| machine | ElfMachine | |
| entry | u64 | |
| flags | u32 | |
| program_headers | list<ProgramHeader> | |
| sections | list<SectionDef> | Sections, index 0 the mandatory `SHT_NULL` entry. |
Append a section and return its index. flags come from section_flags().
A section with its name and contents.
| header | SectionHeader | |
| name | str | |
| data | bytes | Empty for `SHT_NOBITS` sections such as `.bss`. |
A section to be written: like [`SectionHeader`] but owning its name and data, with the file offset and name-table offset filled in at write time.
| name | str | |
| section_type | SectionType | |
| flags | SectionFlags | |
| addr | u64 | Virtual address once loaded (0 for non-`ALLOC` sections). |
| data | bytes | |
| link | u32 | Index of a related section (e.g. a symbol table's string table). |
| info | u32 | |
| addr_align | u64 | |
| entry_size | u64 | Size of each entry for table sections (symbols, relocations), else 0. |
| name | u32 | Offset of the name in the section name string table. |
| section_type | SectionType | |
| flags | SectionFlags | |
| addr | u64 | |
| offset | u64 | |
| size | u64 | |
| link | u32 | |
| info | u32 | |
| addr_align | u64 | |
| entry_size | u64 |
| SectionType::Null | |
| SectionType::ProgBits | |
| SectionType::SymTab | |
| SectionType::StrTab | |
| SectionType::Rela | |
| SectionType::Hash | |
| SectionType::Dynamic | |
| SectionType::Note | |
| SectionType::NoBits | |
| SectionType::Rel | |
| SectionType::ShLib | |
| SectionType::DynSym | |
| SectionType::InitArray | |
| SectionType::FiniArray | |
| SectionType::PreinitArray | |
| SectionType::Group | |
| SectionType::SymTabShndx | |
| SectionType::GnuHash | |
| SectionType::GnuVerDef | |
| SectionType::GnuVerNeed | |
| SectionType::GnuVerSym | |
| SectionType::Other(u32) |
| SegmentType::Null | |
| SegmentType::Load | |
| SegmentType::Dynamic | |
| SegmentType::Interp | |
| SegmentType::Note | |
| SegmentType::ShLib | |
| SegmentType::Phdr | |
| SegmentType::Tls | |
| SegmentType::GnuEhFrame | |
| SegmentType::GnuStack | |
| SegmentType::GnuRelro | |
| SegmentType::GnuProperty | |
| SegmentType::Other(u32) |
> examples
use std::io;
use std::re::elf;
fn main() -> Result<(), str> {
let file = elf::ElfFile::open("/bin/ls")?;
io::println("64-bit: {}", file.is_64());
for name in file.section_names() {
let section = file.section(name).unwrap();
let flags = elf::section_flags_names(section.header.flags);
io::println("{} {}", name, flags.join("|"));
}
for sym in file.symbols()? {
if sym.dynamic && sym.value != 0 {
io::println("{} at {}", sym.name, sym.value.to_hex());
}
}
Ok(())
}