nodejs/node · error · ELFInvalid

unable to parse identification

Error message

unable to parse identification

What it means

Raised by ELFFile.__init__ when struct.unpack fails reading the 16-byte ELF identification field (e_ident). This means the file handle returned fewer than 16 bytes — the file is empty, truncated, or not seekable in the expected way. ELFInvalid is a ValueError subclass, so it can be caught as either.

Source

Thrown at tools/gyp/pylib/packaging/_elffile.py:50

    I386 = 3
    S390 = 22
    Arm = 40
    X8664 = 62
    AArc64 = 183


class ELFFile:
    """
    Representation of an ELF executable.
    """

    def __init__(self, f: IO[bytes]) -> None:
        self._f = f

        try:
            ident = self._read("16B")
        except struct.error:
            raise ELFInvalid("unable to parse identification")
        if (magic := bytes(ident[:4])) != b"\x7fELF":
            raise ELFInvalid(f"invalid magic: {magic!r}")

        self.capacity = ident[4]  # Format for program header (bitness).
        self.encoding = ident[5]  # Data structure encoding (endianness).

        try:
            # e_fmt: Format for program header.
            # p_fmt: Format for section header.
            # p_idx: Indexes to find p_type, p_offset, and p_filesz.
            e_fmt, self._p_fmt, self._p_idx = {
                (1, 1): ("<HHIIIIIHHH", "<IIIIIIII", (0, 1, 4)),  # 32-bit LSB.
                (1, 2): (">HHIIIIIHHH", ">IIIIIIII", (0, 1, 4)),  # 32-bit MSB.
                (2, 1): ("<HHIQQQIHHH", "<IIQQQQQQ", (0, 2, 5)),  # 64-bit LSB.
                (2, 2): (">HHIQQQIHHH", ">IIQQQQQQ", (0, 2, 5)),  # 64-bit MSB.
            }[(self.capacity, self.encoding)]
        except KeyError:
            raise ELFInvalid(

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Verify the file is actually an ELF binary before parsing (check it exists and has a reasonable size).
  2. Open the file in binary mode: open(path, 'rb').
  3. Check the file is not empty or truncated; re-download or rebuild if it is.
  4. Catch ELFInvalid and fall back gracefully if the file might not be ELF.

Example fix

# before
ELFFile(open('not-an-elf.txt'))  # raises ELFInvalid
# after
from packaging._elffile import ELFFile, ELFInvalid
try:
    with open(path, 'rb') as f:
        elf = ELFFile(f)
except ELFInvalid:
    print(f'{path} is not a valid ELF file')
Defensive patterns

Strategy: try-catch

Validate before calling

# Check file size before parsing
import os
if os.path.getsize(path) < 16:
    raise ValueError(f'{path} is too small to be an ELF file')

Type guard

def is_elf_file(path: str) -> bool:
    try:
        with open(path, 'rb') as f:
            return f.read(4) == b'\x7fELF'
    except OSError:
        return False

Try / catch

from packaging._elffile import ELFFile, ELFInvalid
try:
    with open(path, 'rb') as f:
        elf = ELFFile(f)
except ELFInvalid as e:
    print(f'not a valid ELF: {e}')
    elf = None

Prevention

When it happens

Trigger: ELFFile(f) is constructed where f.read(16) returns fewer than 16 bytes, causing struct.unpack('16B', ...) to raise struct.error. The constructor catches it and re-raises as ELFInvalid.

Common situations: Passing a non-ELF file (text file, script, Mach-O binary, PE/COFF exe) to ELFFile. Passing an empty or truncated file. Passing a file opened in text mode instead of binary mode. A download or write that was interrupted mid-stream.

Understand the failure class

Related errors


AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13). Data as JSON: /api/errors/d75645160272391b. Report an issue: GitHub.