ddn.elf.file

ELF file reading infrastructure.

This module provides core functionality for reading and parsing ELF files, supporting both file-backed and memory-backed access with lazy loading and caching for efficient operation.

Types 6

structElfFile

ELF file reader with lazy loading and caching.

This struct provides access to ELF file data, supporting both file-backed and memory-backed sources. It implements lazy loading of sections and segments with caching for efficient access.

Fields
private const(ubyte)[] _data
private types.ElfClass _class
private io.ElfEndian _endian
private gelf.FileHeader _ehdr
private bool _ehdrCached
Methods
types.ElfClass elfClass() pure nothrow @nogc @safe constGet the ELF class (32-bit or 64-bit).
io.ElfEndian endian() pure nothrow @nogc @safe constGet the ELF endianness.
const(ubyte)[] getRawData() pure nothrow @nogc @safe constGet the raw ELF file data.
const(ubyte)[] getDataSlice(size_t offset, size_t size) constGet a slice of the ELF file data.
const(gelf.FileHeader) getEhdr()Get the GELF file header.
elf32.FileHeader getEhdr32()Get the ELF32 file header.
elf64.FileHeader getEhdr64()Get the ELF64 file header.
size_t getSectionCount()Get the number of section headers, resolving extended numbering.
size_t getProgramHeaderCount()Get the number of program headers, resolving extended numbering.
auto getSectionHeaders()Get a range for iterating over section headers.
auto getProgramHeaders()Get a range for iterating over program headers.
private void _detectMetadata()Detect ELF metadata (class and endianness) from data.
private void _loadEhdr()Load and cache the ELF header.
Constructors
this()Default constructor - creates an empty ElfFile.
this(const(ubyte)[] data)Constructor from a byte array (const view).
this(ubyte[] data)Constructor from a mutable byte array.
this(string path)Constructor from file path.
this(File file)Constructor from std.stdio.File.

Input range over section headers, decoding the class-specific wire layout with endian conversion.

Iterates a fixed number of entries at the class-specific stride, decoding each entry on demand.

Fields
private const(ubyte)[] _data
private size_t _offset
private size_t _count
private size_t _stride
private types.ElfClass _class
private io.ElfEndian _endian
Methods
bool empty() constCheck if the range is empty.
gelf.SectionHeader front()Decode and return the current section header.
void popFront()Advance to the next section header.
SectionHeaderRange save()Save the current position.
Constructors
this(const(ubyte)[] data, size_t offset, size_t count, size_t stride, types.ElfClass cls, io.ElfEndian endian)Construct a section header range.

Input range over program headers, decoding the class-specific wire layout (including the differing ELF32 field order) with endian conversion.

Fields
private const(ubyte)[] _data
private size_t _offset
private size_t _count
private size_t _stride
private types.ElfClass _class
private io.ElfEndian _endian
Methods
bool empty() constCheck if the range is empty.
gelf.ProgramHeader front()Decode and return the current program header.
void popFront()Advance to the next program header.
ProgramHeaderRange save()Save the current position.
Constructors
this(const(ubyte)[] data, size_t offset, size_t count, size_t stride, types.ElfClass cls, io.ElfEndian endian)Construct a program header range.

Section range for ElfFile.

Provides input range interface for iterating over sections.

Fields
private ElfFile _file
private SectionHeaderRange _headerRange
Methods
bool empty() @property constCheck if the range is empty.
@property auto front()Get the current section header.
void popFront()Advance to the next section.
typeof(this) save() @propertySave the current position.
Constructors
this(ElfFile file, SectionHeaderRange headerRange)Constructor.

Program range for ElfFile.

Provides input range interface for iterating over program headers.

Fields
private ElfFile _file
private ProgramHeaderRange _headerRange
Methods
bool empty() @property constCheck if the range is empty.
@property auto front()Get the current program header.
void popFront()Advance to the next program header.
typeof(this) save() @propertySave the current position.
Constructors
this(ElfFile file, ProgramHeaderRange headerRange)Constructor.

Builder for creating ELF files from scratch.

This class provides a fluent API for constructing ELF files with full control over sections, segments, symbols, and other ELF structures.

Fields
private types.ElfClass _class
private io.ElfEndian _endian
private types.ElfType _type
private types.ElfMachine _machine
private ulong _entry
private uint _flags
private strtab.StringTableBuilder _shstrtab
private SectionEntry[] _sections
private SegmentEntry[] _segments
private SymbolEntry[] _symbols
private uint _strtabGenIdx
private uint _symtabGenIdx
private ulong _baseAddress
private types.ElfOsAbi _osabi
private ubyte _abiVersion
private uint _phnum
Methods
private uint toUint32(ulong value, string field) staticConvert a 64-bit value to its 32-bit ELF representation.
private ushort toSymbolShndx(uint index) staticConvert a symbol section index to its 16-bit ELF representation.
private void updateOrAddSymtab(ubyte[] symData, uint strtabIdx, uint firstGlobal, ulong addralign, ulong entsize)Create or update the generated .symtab section in place.
bool validate() constValidate the internal state of this builder.
ElfBuilder create(types.ElfClass class_, io.ElfEndian endian) staticCreate a new ELF builder with the specified class and endianness.
ElfBuilder setFileType(types.ElfType type)Set the ELF file type (relocatable, executable, shared object, etc.).
ElfBuilder setMachine(types.ElfMachine machine)Set the target machine architecture.
ElfBuilder setEntryPoint(ulong entry)Set the entry point address.
ElfBuilder setBaseAddress(ulong base)Set the virtual address base for the file layout.
ElfBuilder setOsAbi(types.ElfOsAbi osabi)Set the OS/ABI identification.
ElfBuilder setAbiVersion(ubyte ver)Set the ABI version.
ubyte abiVersion() const
void setGeneratedTableIndices(uint symtabIdx, uint strtabIdx)
ElfBuilder setSectionAddress(uint index, ulong addr)Set the virtual address of a section.
ElfBuilder setSegmentAddress(uint index, ulong vaddr, ulong paddr = 0)Set the virtual (and optional physical) address of a segment.
ulong baseAddress() constGet the virtual address base used for the layout.
types.ElfClass elfClass() constGet the ELF class this builder was created with.
io.ElfEndian endian() constGet the byte order this builder was created with.
ElfBuilder setFlags(uint flags)Set processor-specific flags.
uint addSection(string name, uint type, ulong flags, const(ubyte)[] data = null, uint link = 0, uint info = 0, ulong addralign = 1, ulong entsize = 0, ulong memSize = 0)Add a section to the ELF file.
uint addSegment(uint type, uint flags, uint[] sections, ulong alignment = 0x1000)Add a segment (program header) to the ELF file.
ElfBuilder addSymbol(string name, uint sectionIndex, uint binding, uint type, ulong value = 0, ulong size = 0)Add a symbol to the symbol table.
ubyte[] build()Build the ELF file and return the raw byte array.
void write(string path)Build the ELF file and write it to a file.
void write(File file)Build the ELF file and write it to an open file.
uint getSectionCount()Get the number of sections.
uint getSegmentCount()Get the number of segments.
void updateSectionData(uint index, const(ubyte)[] data)Update the data of an existing section.
void removeSegmentAt(uint index)Remove a segment at the specified index.
Constructors
this()Validate the internal state of this builder.
Nested Templates
SectionEntryInternal representation of a section being constructed by ElfBuilder.
SegmentEntryInternal representation of a segment being constructed by ElfBuilder.
SymbolEntryInternal representation of a symbol being added to ElfBuilder.

Functions 13

fnbool isElfFile(const(ubyte)[] data) pure nothrow @nogc @safeCheck if the given data represents a valid ELF file.
fntypes.ElfClass getElfClass(const(ubyte)[] data)Get the ELF class (32-bit or 64-bit) from ELF data.
fntypes.ElfData getElfEndian(const(ubyte)[] data)Get the ELF endianness from ELF data.
fnbool validateElfHeader(const ref gelf.FileHeader hdr)Validate an ELF file header.
private fnsize_t sectionHeaderSize(types.ElfClass cls)On-disk size of a section header for the given ELF class.
private fnsize_t programHeaderSize(types.ElfClass cls)On-disk size of a program header for the given ELF class.
private fnsize_t countSections(const(ubyte)[] data)Count the section headers serialized in a built file.
private fnvoid writeFileHeader32(ubyte[] data, ref const elf32.FileHeader ehdr, io.ElfEndian endian)Write ELF32 file header with endianness handling.
private fnvoid writeFileHeader64(ubyte[] data, ref const elf64.FileHeader ehdr, io.ElfEndian endian)Write ELF64 file header with endianness handling.
private fnvoid writeSectionHeader32(ubyte[] data, size_t pos, ref const elf32.SectionHeader shdr, io.ElfEndian endian)Write ELF32 section header with endianness handling.
private fnvoid writeSectionHeader64(ubyte[] data, size_t pos, ref const elf64.SectionHeader shdr, io.ElfEndian endian)Write ELF64 section header with endianness handling.
private fnvoid writeProgramHeader32(ubyte[] data, size_t pos, ref const elf32.ProgramHeader phdr, io.ElfEndian endian)Write ELF32 program header with endianness handling.
private fnvoid writeProgramHeader64(ubyte[] data, size_t pos, ref const elf64.ProgramHeader phdr, io.ElfEndian endian)Write ELF64 program header with endianness handling.