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
ElfBuilder _builder
ElfFile _loadedFile
Methods
ElfEditor create(ElfClass class_, ElfEndian endian)Create a new ELF editor.
ElfEditor load(string path)Load an existing ELF file 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.
ElfEditor 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)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).
void _copySections(ref ElfFile file, ref ElfBuilder builder)Copy sections from an ElfFile to an ElfBuilder.
void _copySegments(ref ElfFile file, ref ElfBuilder builder)Copy segments from an ElfFile to an ElfBuilder.
void _copySymbols(ref ElfFile file, ref ElfBuilder builder)Copy symbols from an ElfFile to an ElfBuilder.
Constructors
this(ElfBuilder builder, ElfFile loadedFile = ElfFile.init)Private constructor to ensure factory method usage.