ddn.elf.editor

ddn.elf.editor - ELF File Editor Module

This module provides the ElfEditor class for modifying ELF files. It wraps ElfBuilder to provide a convenient interface for editing ELF files, including loading existing files and making modifications.

class ElfEditor

Types 1

ELF File Editor class.

Provides methods to load, modify, and save ELF files. Wraps an internal ElfBuilder instance to handle the actual file creation.

Example:

// Create a new ELF file
auto editor = ElfEditor.create(ElfClass.CLASS64, ElfEndian.LITTLE);
editor.setFileType(ElfType.REL)
       .setMachine(ElfMachine.X86_64)
       .addSection(".text", SectionType.PROGBITS, SectionFlags.ALLOC | SectionFlags.EXECINSTR,
                  [0xC3, 0x90]);
editor.write("output.elf");

// Load and modify an existing file
auto editor2 = ElfEditor.load("input.elf");
editor2.setEntry(0x400000);
editor2.write("output.elf");

Fields
private ElfBuilder _builder
private ElfFile _loadedFile
private bool _isLoaded
private ubyte[] _modifiedData
private bool _layoutDirty
private ElfClass _class
private ElfEndian _endian
Methods
ElfEditor create(ElfClass class_, ElfEndian endian) staticCreate a new ELF editor.
ElfEditor load(string path) staticLoad an existing ELF file for editing.
ElfEditor load(ubyte[] data) staticLoad an existing ELF file from raw bytes for editing.
ElfEditor setFileType(ElfType type)Set the ELF file type.
ElfEditor setMachine(ElfMachine machine)Set the target machine architecture.
ElfEditor setEntry(ulong entry)Set the entry point address.
private void _updateEntryInModifiedData(ulong entry)
ElfEditor setBaseAddress(ulong base)Set the virtual address base for the file layout.
ElfEditor setSectionAddress(uint index, ulong addr)Set the virtual address of a section.
ElfEditor setSegmentAddress(uint index, ulong vaddr, ulong paddr = 0)Set the virtual (and optional physical) address of a segment.
ElfEditor setFlags(uint flags)Set processor-specific flags.
ElfEditor setOsAbi(ElfOsAbi osabi)Set the OS/ABI identification.
ElfEditor setAbiVersion(ubyte ver)Set the ABI version.
uint addSection(string name, uint type, ulong flags, const(ubyte)[] data = null, uint link = 0, uint info = 0, ulong addralign = 1, ulong entsize = 0)Add a section to the ELF file.
ElfEditor removeSection(uint index)Remove a section from the ELF file.
ElfEditor modifySectionData(uint index, const(ubyte)[] data)Modify the data of an existing section.
uint addSegment(uint type, uint flags, uint[] sections, ulong alignment = 0x1000)Add a segment (program header) to the ELF file.
ElfEditor removeSegment(uint index)Remove a segment from the ELF file.
ElfEditor addSymbol(string name, uint sectionIndex, uint binding, uint type, ulong value = 0, ulong size = 0)Add a symbol to the symbol table.
ElfEditor removeSymbol(uint index)Remove a symbol from the symbol table.
ubyte[] build()Build the ELF file and return the raw byte array.
void write(string path)Write the ELF file to disk.
void write(File file)Write the ELF file to a std.stdio.File.
uint getSectionCount()Get the number of sections.
uint getSegmentCount()Get the number of segments.
bool validate()Validate the ELF file structure.
ElfBuilder getBuilder()Get the underlying ElfBuilder.
ElfFile getLoadedFile()Get the loaded ELF file (if any).
private void _copySections(ref ElfFile file, ref ElfBuilder builder) staticCopy sections from an ElfFile to an ElfBuilder.
private void _copySegments(ref ElfFile file, ref ElfBuilder builder) staticCopy segments from an ElfFile to an ElfBuilder.
private void _copySymbols(ref ElfFile file, ref ElfBuilder builder) staticCopy symbols from an ElfFile to an ElfBuilder.
Constructors
this(ElfBuilder builder, ElfFile loadedFile = ElfFile.init)Private constructor to ensure factory method usage.