elfie 0.1.0
elfie: ^0.1.0 copied to clipboard
A Dart library for parsing and analyzing ELF (Executable and Linkable Format) files.
// Example file - print statements are intentional for demonstration.
// ignore_for_file: avoid_print
import 'package:elfie/elfie.dart';
void main(List<String> args) {
if (args.isEmpty) {
print('Usage: dart run example/elfie_example.dart <elf_file>');
return;
}
final path = args[0];
// Use the non-throwing API for graceful error handling
final result = Elfie.tryParseFile(path);
if (result.isFailure) {
_printError(result.error);
return;
}
final elf = result.value;
print('ELF File: $path');
print('');
print('=== 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)}');
print('');
print('=== Program Headers (${elf.programHeaders.length}) ===');
for (final ph in elf.programHeaders) {
print(
' [${ph.index}] ${ph.typeName.padRight(12)} '
'Offset: 0x${ph.offset.toRadixString(16).padLeft(8, '0')} '
'VAddr: 0x${ph.virtualAddress.toRadixString(16).padLeft(16, '0')} '
'Flags: ${ph.flagsString}',
);
}
print('');
print('=== Section Headers (${elf.sectionHeaders.length}) ===');
for (final sh in elf.sectionHeaders) {
print(
' [${sh.index.toString().padLeft(2)}] ${sh.name.padRight(20)} '
'${sh.typeName.padRight(12)} '
'Size: 0x${sh.size.toRadixString(16).padLeft(8, '0')} '
'Flags: ${sh.flagsString}',
);
}
}
void _printError(Object error) {
switch (error) {
case ElfFormatException():
print('Error: Invalid ELF format - ${error.message}');
if (error.field != null) {
print('Field: ${error.field}');
}
case ElfTruncatedFileException():
print('Error: File truncated - ${error.message}');
if (error.expectedBytes != null && error.actualBytes != null) {
print(
'Expected ${error.expectedBytes} bytes, '
'got ${error.actualBytes}',
);
}
case ElfCorruptDataException():
print('Error: Corrupt data - ${error.message}');
if (error.offset != null) {
print('At offset: 0x${error.offset!.toRadixString(16)}');
}
case ElfException():
print('Error: ${error.message}');
default:
print('Error: $error');
}
}