nodejs/node · error · ELFInvalid

unable to parse machine and section information

Error message

unable to parse machine and section information

What it means

Raised by ELFFile.__init__ when struct.unpack fails reading the ELF header fields after the 16-byte identification (e_type, e_machine, e_version, etc.). The identification block parsed fine, but the remaining header bytes are fewer than expected, indicating a truncated ELF file — it has a valid magic but is too short to be complete.

Source

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

                f"unrecognized capacity ({self.capacity}) or "
                f"encoding ({self.encoding})"
            )

        try:
            (
                _,
                self.machine,  # Architecture type.
                _,
                _,
                self._e_phoff,  # Offset of program header.
                _,
                self.flags,  # Processor-specific flags.
                _,
                self._e_phentsize,  # Size of section.
                self._e_phnum,  # Number of sections.
            ) = self._read(e_fmt)
        except struct.error as e:
            raise ELFInvalid("unable to parse machine and section information") from e

    def _read(self, fmt: str) -> Tuple[int, ...]:
        return struct.unpack(fmt, self._f.read(struct.calcsize(fmt)))

    @property
    def interpreter(self) -> Optional[str]:
        """
        The path recorded in the ``PT_INTERP`` section header.
        """
        for index in range(self._e_phnum):
            self._f.seek(self._e_phoff + self._e_phentsize * index)
            try:
                data = self._read(self._p_fmt)
            except struct.error:
                continue
            if data[self._p_idx[0]] != 3:  # Not PT_INTERP.
                continue
            self._f.seek(data[self._p_idx[1]])

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Re-download or rebuild the ELF file — it is truncated.
  2. Check the file size against the expected minimum for a valid ELF (at least ~52 bytes for 32-bit, ~64 for 64-bit).
  3. Wrap ELFFile construction in try/except ELFInvalid and skip corrupt files.
  4. Verify integrity with readelf -h; it will report 'Error: Unable to read the ELF header'.

Example fix

import os
path = 'libfoo.so'
if os.path.getsize(path) < 64:
    print('file too small to be a complete ELF')
else:
    try:
        elf = ELFFile(open(path, 'rb'))
    except ELFInvalid:
        print('truncated or corrupt ELF')
Defensive patterns

Strategy: try-catch

Validate before calling

# Ensure the file is large enough for a complete ELF header
import os, struct
min_header = 52  # 32-bit; 64-bit needs 64
if os.path.getsize(path) < min_header:
    raise ValueError(f'{path} is truncated (< {min_header} bytes)')

Type guard

def is_complete_elf(path: str) -> bool:
    import os
    # 32-bit ELF header = 52 bytes; 64-bit = 64 bytes
    return os.path.getsize(path) >= 64 and open(path, 'rb').read(4) == b'\x7fELF'

Try / catch

from packaging._elffile import ELFFile, ELFInvalid
try:
    elf = ELFFile(open(path, 'rb'))
except ELFInvalid as e:
    if 'machine and section' in str(e):
        print('truncated ELF header')
    raise

Prevention

When it happens

Trigger: The file has a valid \x7fELF magic and valid capacity/encoding, but f.read() for the e_fmt struct returns fewer bytes than calcsize(e_fmt) requires, raising struct.error. This means the file is longer than 16 bytes but shorter than a full ELF header.

Common situations: A partially downloaded or partially written ELF file. A file that was the target of a truncated copy. A crafted malformed binary used in security testing. A file with a valid ELF header prefix but corrupted body.

Understand the failure class

Related errors


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