Skip to main content

dwarffi is a Python library for parsing ISF files and providing an interface to access kernel symbols and types.

Project description

dwarffi

An authentic, DWARF-powered type interface for Python.

dwarffi allows you to interact with memory layouts using Intermediate Symbol Files (ISF) generated from DWARF debug information. It provides a CFFI-like experience without requiring header files---instead, it uses the actual compiled structures from your binaries.

Intermediate Symbol Files (ISF) are JSON representations of DWARF type information. They capture the exact memory layout, including padding and alignment, as defined in the original C code and compiled into the binary. You can generate ISF files using the dwarf2json tool, which extracts DWARF info from ELF binaries.

Read more about dwarf2json and ISF in the dwarf2json README.


🚀 Features

  • DWARF-Native: No need to rewrite C headers. Just point to a .json or .json.xz ISF file (generated by dwarf2json).
  • Architecture Agnostic: Handles Big Endian (PowerPC/MIPS) and Little Endian (ARM/x86) transparently.
  • Dynamic cdef: Compile C code on the fly to generate types, with automatic debug-type retention to prevent compilers from stripping unused definitions.
  • Recursive Typedef Handling: Automatic resolution and decay of typedef chains.
  • C-Style Magic:
    • Pointer arithmetic (ptr + 5)
    • Pointer subtraction (ptr2 - ptr1)
    • Array slicing (arr[1:5])
    • Deep struct initialization via nested dictionaries
  • Safety Semantics:
    • Automatic bit-masking
    • Sign extension
    • C-style integer overflow/underflow behavior
  • Anonymous Struct/Union Flattening: Access anonymous union members directly (ideal for register maps).
  • ISF Export Support: Save dynamically generated ISFs to .json or .json.xz.

📦 Installation

pip install dwarffi

Requirements for cdef()

To use dynamic compilation:

  • A C compiler (gcc, clang, or cross-compiler)
  • dwarf2json available in your PATH

NOTE: For some compilers you may need to disable options for optimizing away unused debug types. For example, with gcc you can use -fno-eliminate-unused-debug-types.


🛠️ Quick Start

1️⃣ CFFI-style cdef

from dwarffi import DFFI

ffi = DFFI()
ffi.cdef("""
    struct sensor_data {
        uint32_t timestamp;
        int16_t  readings[3];
        uint8_t  status;
    };
""")

Create an instance:

sensor = ffi.new("struct sensor_data", {
    "timestamp": 1234567,
    "readings": [10, -5, 20],
    "status": 0x01
})

print(f"Bytes: {ffi.to_bytes(sensor).hex()}")
print(f"Reading[1]: {sensor.readings[1]}")  # -5

2️⃣ Embedded Architecture Analysis

ffi = DFFI("mips_be_target.json")

raw_data = bytearray(b'\x00\x00\x12\x34\xff\xff\xff\xfb')
cpu_ctx = ffi.from_buffer("struct cpu_context", raw_data)

print(hex(cpu_ctx.pc))      # 0x1234
print(cpu_ctx.status)       # -5 (Sign-extended)

🧩 Advanced Usage

Anonymous Unions

ffi.cdef("""
struct reg_map {
    union {
        uint32_t ALL;
        struct {
            uint16_t LOW;
            uint16_t HIGH;
        };
    };
};
""")

reg = ffi.new("struct reg_map")
reg.ALL = 0x12345678
print(hex(reg.HIGH))  # 0x1234

Pointer Arithmetic

ptr = ffi.cast("int *", 0x4000)
next_ptr = ptr + 1
print(hex(next_ptr.address))

⚙️ How It Works

dwarffi operates in three phases:

1. Parsing

Reads ISF JSON files that represent the DWARF type tree.

2. Type Synthesis

Builds Python representations of:

  • Base types
  • Structs
  • Unions
  • Enums
  • Typedef chains
  • Arrays
  • Pointers

3. Memory Mapping

Uses Python's struct.pack and struct.unpack under the hood to:

  • Convert Python integers into architecture-accurate byte layouts
  • Apply endianness rules
  • Respect alignment and padding

Instances are bound views into bytearrays. Field access directly reads/writes into the underlying buffer.


📚 Core API Reference

DFFI()

Create an empty instance or load an ISF file.

cdef(source, compiler="gcc", save_isf_to=None)

Compile C source → DWARF → ISF → load into current FFI.

new(ctype, init=None)

Allocate a new instance of a C type.

from_buffer(ctype, buffer)

Bind a type to existing memory.

sizeof(ctype)

Return size in bytes.

offsetof(ctype, field)

Return byte offset of a field.

cast(ctype, value)

Reinterpret memory or create pointer instances.

addressof(instance)

Return pointer to instance.


🤝 Contributing

Contributions are welcome!

python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest

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

dwarffi-0.0.6.tar.gz (41.1 kB view details)

Uploaded Source

Built Distribution

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

dwarffi-0.0.6-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file dwarffi-0.0.6.tar.gz.

File metadata

  • Download URL: dwarffi-0.0.6.tar.gz
  • Upload date:
  • Size: 41.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dwarffi-0.0.6.tar.gz
Algorithm Hash digest
SHA256 df78266c7405a800b0b5524d0dcc7b752af39590bc99babbe43b03d183ebce32
MD5 588615fc0da43f9df37276d4eae603a1
BLAKE2b-256 5e783f31ece4c9899dc976437db532509848d18c5959da3c55c8399fd61ad687

See more details on using hashes here.

Provenance

The following attestation bundles were made for dwarffi-0.0.6.tar.gz:

Publisher: publish.yml on rehosting/dwarffi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dwarffi-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: dwarffi-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 22.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dwarffi-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 db72e5e55471f3da1ddb197ce07d6dc3b067a068f64337c6b82ba4c0d44040dd
MD5 f9abcd5f65df26edcc6967bf1d6fe094
BLAKE2b-256 0b29c23b8479e8471999fe6db8312d3e89a79433affecd5aaf2586d51134af04

See more details on using hashes here.

Provenance

The following attestation bundles were made for dwarffi-0.0.6-py3-none-any.whl:

Publisher: publish.yml on rehosting/dwarffi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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