Skip to main content

Pure Python ELF file parser

Project description

woodelf

Pure-Python ELF file parser and editor. Reads and rewrites 32/64-bit ELF binaries via a small set of editor classes, each scoped to one structural region of the file (header, section headers, symbols, dynamic entries, …).

Requires GNU binutils (objcopy, objdump, readelf) on PATH, plus hexdump and sh. Python ≥ 3.9.

Install

pip install -e .

Quick start

import woodelf
from woodelf import (
    ElfHeaderEditor,
    SectionHeaderEditor,
    SymbolEditor,
    DynamicEntryEditor,
    SECTION,
)

elf = woodelf.parse("/path/to/binary")

# Inspect the ELF header
hdr = ElfHeaderEditor(elf).read_elf_header()
print(hdr.typ, hex(hdr.entry))

# Walk section headers
for sh in SectionHeaderEditor(elf).read_section_header_table():
    print(sh.name, hex(sh.addr), sh.siz)

# Mutate a symbol's value, then save to a new path
syms_editor = SymbolEditor(elf, SECTION.SYMTAB)
syms = syms_editor.read_symbol_table()
main = next(s for s in syms if s.name == "main")
main.value = 0x401234
syms_editor.write_symbol_table(syms)

elf.write("/path/to/output")

API

from woodelf import (
    parse, Elf, MalformedElfError, SECTION, gnu_hash,
    ElfHeaderEditor, SectionHeaderEditor, ProgramHeaderEditor,
    SymbolEditor, StrTabEditor, DynamicEntryEditor, SymbolVersionEditor,
)

parse(path, toolchain_path=None, prefix='') -> Elf | None

Open and parse an ELF file. Returns None if the file isn't an ELF (bad magic, truncated e_ident). toolchain_path / prefix let woodelf shell out to a cross-toolchain (prefix + 'readelf', prefix + 'objcopy', …) when needed — useful for embedded targets.

Elf

Handle to a parsed ELF. The class itself exposes file-level concerns; structural elements are accessed through their respective editors (see below).

Attribute / method What it gives you
Elf.from_path(path, ...) Same as parse(...)
elf.unit ELF32 or ELF64 (size enum used for serialization)
elf.endian 'little' or 'big'
elf.revisions List of file paths; each edit produces a new revision
elf.get_current_revision() Path to the latest revision
elf.write(path) Copy the current revision to path (creates parent dirs)
elf.iter_objdump_sections() Yields the section summary that objdump -h produces

Editors

Each editor stages mutations against one region of the ELF. Read methods return element objects (ElfHeader, SectionHeader, Symbol, DynamicEntry, …); write methods serialize them back. Most write paths are in-place (direct file I/O); content-changing writes (e.g. resizing a section) go through objcopy --update-section, which appends a new revision to elf.revisions.

Editor Constructed as Edits
ElfHeaderEditor ElfHeaderEditor(elf) ELF header fields
SectionHeaderEditor SectionHeaderEditor(elf) Section header table
ProgramHeaderEditor ProgramHeaderEditor(elf) Program header table
SymbolEditor SymbolEditor(elf, SECTION.SYMTAB) or .DYNSYM .symtab / .dynsym entries
StrTabEditor StrTabEditor(elf, SECTION.STRTAB) etc. .strtab / .dynstr / .shstrtab strings
DynamicEntryEditor DynamicEntryEditor(elf) .dynamic entries
SymbolVersionEditor SymbolVersionEditor(elf) .gnu.version, .gnu.version_d/r

Elements

Concrete types returned by the editors (see woodelf.elements):

  • Header: ElfHeader, E_Ident
  • Sections / segments: SectionHeader, SectionHeaderTable, ProgramHeader
  • Symbols: Symbol, SymbolTable (with defined_symbols() / needed_symbols() filters)
  • Dynamic linker: DynamicEntry
  • Symbol versioning: Verdef, Verdaux, Verneed, Vernaux, Version, VerdefTable, VerneedTable, VerauxTable, VersionTable
  • Hashing: GNU hash via woodelf.gnu_hash(name)

Errors

MalformedElfError — raised when the file fails structural validation (e.g. a section header table that runs past the end of the file).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

woodelf-0.1.2.tar.gz (38.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

woodelf-0.1.2-py3-none-any.whl (40.2 kB view details)

Uploaded Python 3

File details

Details for the file woodelf-0.1.2.tar.gz.

File metadata

  • Download URL: woodelf-0.1.2.tar.gz
  • Upload date:
  • Size: 38.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for woodelf-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8a0b254e03973c8cb121c933406e1f388e692cb0d7b4354762e7a0f4ea74bfea
MD5 52f0dfde697d9ce7df92634c9cad274a
BLAKE2b-256 0749c4a2dd44df031903c3aef69d7c8d24ed916df855d86d67e9721d22980fdb

See more details on using hashes here.

File details

Details for the file woodelf-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: woodelf-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 40.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for woodelf-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8ed5a13df1ad0afda296ce287b4dcae704794ef11aedc135f39877afde007326
MD5 88efb5e08edccbed9842c88d7844ba54
BLAKE2b-256 d31692066f26a5e09e530474d4c0afadb799cdc771935d6b134b342ac46b7a76

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page