Skip to content

Repository files navigation

elfie

A pure Dart library for parsing ELF (Executable and Linkable Format) files.

elfie provides a clean, type-safe API for reading and analyzing ELF binaries, supporting both 32-bit and 64-bit formats in little-endian and big-endian byte orders.

Features

  • Comprehensive Parsing:
    • ELF Headers (File type, machine, entry point, etc.)
    • Program Headers (Segments)
    • Section Headers
    • Symbol Tables (.symtab, .dynsym)
    • Dynamic Sections
  • Cross-Platform Support: Handle ELF32 and ELF64 files from any architecture (x86, ARM, RISC-V, etc.).
  • Endianness Aware: Seamlessly parses little-endian and big-endian files.
  • Zero Dependencies: Written in pure Dart.

Getting started

Add elfie to your pubspec.yaml:

dependencies:
elfie: ^1.0.0

Or run:

dart pub add elfie

Usage

Quick Start

The simplest way to parse an ELF file:

import'package:elfie/elfie.dart';
voidmain() {
// Parse from bytesfinal elf =Elfie.parse(bytes);
// Or parse from a file pathfinal elf =Elfie.parseFile('/path/to/binary');
print('Entry Point: 0x${elf.header.entryPoint.toRadixString(16)}');
}

Non-Throwing API

Use tryParse and tryParseFile for error handling without exceptions:

import'package:elfie/elfie.dart';
voidmain() {
final result =Elfie.tryParseFile('/path/to/binary');
if (result.isSuccess) {
final elf = result.value;
print('Parsed successfully!');
print('Class: ${elf.header.elfClass.displayName}');
} else {
print('Error: ${result.error}');
}
}

The Result<T> type provides:

  • isSuccess / isFailure - Check the result status
  • value - Get the parsed ElfFile (throws if failure)
  • error - Get the error object (throws if success)
  • getOrElse(fallback) - Get value or compute fallback from error

Inspecting Headers

final elf =Elfie.parseFile('/path/to/binary');
print('=== ELF Header ===');
print('Class: ${elf.header.elfClass.displayName}');
print('Endianness: ${elf.header.elfEndianness.displayName}');
print('Type: ${elf.header.typeName}');
print('Machine: ${elf.header.machineName}');
print('Entry: 0x${elf.header.entryPoint.toRadixString(16)}');

Inspecting Sections

for (final section in elf.sectionHeaders) {
print('Section: ${section.name}');
print(' Type: ${section.typeName}');
print(' Size: ${section.size} bytes');
print(' Address: 0x${section.virtualAddress.toRadixString(16)}');
}

Inspecting Program Headers

for (final ph in elf.programHeaders) {
print('Segment: ${ph.typeName}');
print(' Offset: 0x${ph.offset.toRadixString(16)}');
print(' VAddr: 0x${ph.virtualAddress.toRadixString(16)}');
print(' Flags: ${ph.flagsString}');
}

Exception-Based Error Handling

If you prefer exceptions, use parse and parseFile:

try {
final elf =Elfie.parseFile('/path/to/binary');
} onElfFormatExceptioncatch (e) {
print('Invalid ELF format: ${e.message}');
} onElfTruncatedFileExceptioncatch (e) {
print('File is incomplete. Expected ${e.expectedBytes} bytes.');
} onElfCorruptDataExceptioncatch (e) {
print('Corrupt data encountered at offset ${e.offset}.');
} onFileSystemExceptioncatch (e) {
print('Could not read file: ${e.message}');
}

Low-Level Parser Access

For more control, use ElfParser directly:

final parser =ElfParser();
// Parse from bytesfinal elf = parser.parseBytes(bytes);
// Parse from filefinal elf = parser.parseFile('/path/to/binary');

API Reference

Elfie (Recommended)

MethodDescription
Elfie.parse(bytes)Parse ELF from bytes. Throws on error.
Elfie.parseFile(path)Parse ELF from file. Throws on error.
Elfie.tryParse(bytes)Parse ELF from bytes. Returns Result<ElfFile>.
Elfie.tryParseFile(path)Parse ELF from file. Returns Result<ElfFile>.

Result<T>

MemberDescription
isSuccesstrue if parsing succeeded.
isFailuretrue if parsing failed.
valueThe parsed ElfFile. Throws StateError if failure.
errorThe error object. Throws StateError if success.
stackTraceStack trace of the error, if available.
getOrElse(fn)Returns value, or result of fn(error) if failure.

Exceptions

ExceptionDescription
ElfExceptionBase class for all ELF parsing errors.
ElfFormatExceptionInvalid ELF format (bad magic, unsupported version).
ElfTruncatedFileExceptionFile is too short or truncated.
ElfCorruptDataExceptionData is corrupt or malformed.
ElfUnsupportedFeatureExceptionUnsupported ELF feature encountered.

Additional information

For more details, see the example/ folder.

About

A Dart library for parsing and analyzing ELF (Executable and Linkable Format) files.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages