xur_

> module

std::re::elf

read and build ELF files

use std::re::elf; feature re-image in the browser

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.

# section_flags(names: list<str>) -> Result<u64, str>

Combine section flag names, such as ["ALLOC",\"EXECINSTR"], into the integer for a section header. An unknown name gives Err.

# section_flags_names(bits: u64) -> list<str>

The names of the section flags set in bits, for example in section.header.flags.

# segment_flags(names: list<str>) -> Result<u32, str>

Combine segment flag names into the integer for a program header.

# segment_flags_names(bits: u32) -> list<str>

The names of the segment flags set in bits, for example in a program header's flags.

> types

┌ enum ElfClass
ElfClass::Elf32
ElfClass::Elf64
┌ enum ElfData
ElfData::Little
ElfData::Big
┌ struct ElfFile
headerElfHeader
program_headerslist<ProgramHeader>
sectionslist<Section>
file_bytesbytes
# ElfFile::open(path: str) -> Result<ElfFile, str>

Read and parse the ELF file at path.

# ElfFile::parse(data: bytes) -> Result<ElfFile, str>

Parse an ELF file held in memory.

# elf_file.find_symbol(name: str) -> Result<Option<Symbol>, str>

The first symbol named name.

# elf_file.is_64() -> bool

Whether it is a 64-bit (ELFCLASS64) file.

# elf_file.offset_to_vaddr(offset: usize) -> Option<u64>

The virtual address that file offset offset is loaded at, or None.

# elf_file.read(vaddr: u64, len: usize) -> Option<bytes>

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).

# elf_file.relocated_pointers() -> Result<list<(u64, u64)>, str>

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.

# elf_file.relocations() -> Result<list<Relocation>, str>

The relocations of every SHT_REL and SHT_RELA section.

# elf_file.section(name: str) -> Option<Section>

The first section named name, with its header and data.

# elf_file.section_names() -> list<str>

The section names, in table order, such as ".text".

# elf_file.symbols() -> Result<list<Symbol>, str>

The symbols of .symtab and .dynsym. A symbol's dynamic field says which table it came from.

# elf_file.vaddr_to_offset(vaddr: u64) -> Option<usize>

The file offset of virtual address vaddr, through the PT_LOAD segments, or None if no segment maps it.

┌ struct ElfHeader
classElfClass
byte_orderElfData
os_abiu8
abi_versionu8
file_typeElfType
machineElfMachine
versionu32
entryu64
program_header_offsetu64
section_header_offsetu64
flagsu32
header_sizeu16
program_header_entry_sizeu16
program_header_countu16
section_header_entry_sizeu16
section_header_countu16
section_name_indexu16Index of the section holding section names.
┌ enum ElfMachine
ElfMachine::None
ElfMachine::X86
ElfMachine::Arm
ElfMachine::X86_64
ElfMachine::AArch64
ElfMachine::RiscV
ElfMachine::Other(u16)
┌ enum ElfType
ElfType::None
ElfType::RelRelocatable object file.
ElfType::ExecExecutable.
ElfType::DynShared object (also position independent executables).
ElfType::Core
ElfType::Other(u16)
┌ struct ImageDef

An owned, writable ELF image.

classElfClass
byte_orderElfData
os_abiu8
abi_versionu8
file_typeElfType
machineElfMachine
entryu64
flagsu32
program_headerslist<ProgramHeader>
sectionslist<SectionDef>Sections, index 0 the mandatory `SHT_NULL` entry.
# ImageDef::new_shared(class: ElfClass, machine: ElfMachine) -> ImageDef

A new, empty shared object (ET_DYN, little-endian, System V ABI) for machine.

# image_def.add_section(name: str, section_type: SectionType, flags: u64, data: bytes) -> usize

Append a section and return its index. flags come from section_flags().

# image_def.section_index(name: str) -> Option<usize>

The index of the section named name.

# image_def.write() -> Result<bytes, str>

Serialize the object: lay out the sections, add a .shstrtab, and write the headers and data.

┌ struct ProgramHeader
segment_typeSegmentType
flagsSegmentFlags
offsetu64
vaddru64
paddru64
file_sizeu64
memory_sizeu64
alignu64
┌ struct RelocatedPointers

Pointer values from [`ElfFile::relocated_pointers`], sorted by address.

0list<(u64, u64)>
┌ struct Section

A section with its name and contents.

headerSectionHeader
namestr
databytesEmpty for `SHT_NOBITS` sections such as `.bss`.
┌ struct SectionDef

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.

namestr
section_typeSectionType
flagsSectionFlags
addru64Virtual address once loaded (0 for non-`ALLOC` sections).
databytes
linku32Index of a related section (e.g. a symbol table's string table).
infou32
addr_alignu64
entry_sizeu64Size of each entry for table sections (symbols, relocations), else 0.
┌ struct SectionHeader
nameu32Offset of the name in the section name string table.
section_typeSectionType
flagsSectionFlags
addru64
offsetu64
sizeu64
linku32
infou32
addr_alignu64
entry_sizeu64
┌ enum SectionType
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)
┌ enum SegmentType
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)
┌ struct Symbol
namestr
name_offsetu32
valueu64
sizeu64
infou8
otheru8
section_indexu16
dynamicboolFrom `.dynsym` rather than `.symtab`.

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