pypa/pip · 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` of the program-header format string (`e_fmt`) fails after the e_ident/class/encoding were valid. This means the file had a plausible ELF signature but is too short or malformed to contain the program header table metadata (machine type, phoff, phnum, etc.).
Source
Thrown at src/pip/_vendor/packaging/_elffile.py:88
raise ELFInvalid(
f"unrecognized capacity ({self.capacity}) or encoding ({self.encoding})"
) from e
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) -> str | None:
"""
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 d7d0d0a394)
Solutions
- Verify file size meets the minimum ELF header size (52 bytes for 32-bit, 64 for 64-bit) before parsing
- Re-download or rebuild the binary; the header is structurally broken
- Catch ELFInvalid and report the file as corrupt rather than crashing
Example fix
// before
elf = ELFFile(f)
// after
import os
if os.path.getsize(path) < 64:
raise ValueError(f'{path} truncated, cannot be a complete ELF')
try:
elf = ELFFile(f)
except ELFInvalid as e:
raise ValueError(f'{path} corrupt ELF: {e}') from e Defensive patterns
Strategy: try-catch
Validate before calling
import os
def is_full_elf_header(path: str) -> bool:
# 52 bytes for 32-bit, 64 for 64-bit ELF header
return os.path.getsize(path) >= 64 Type guard
def is_plausible_elf_size(size: int) -> bool:
return isinstance(size, int) and size >= 64 Try / catch
from packaging._elffile import ELFFile, ELFInvalid
try:
elf = ELFFile(f)
except ELFInvalid as e:
if 'machine and section' in str(e):
log.warning('%s has truncated ELF header', path)
continue
raise Prevention
- Verify file size >= 64 bytes before ELF parsing
- Re-download binaries that fail header parsing rather than retrying
- Catch ELFInvalid distinct from generic exceptions for clear error messages
When it happens
Trigger: A file that is at least 16 bytes with valid magic but truncated before the full ELF header (e.g. exactly 16-52 bytes); a header that was partially overwritten; an `e_ident` that happens to match `\x7fELF` by coincidence in a non-ELF blob.
Common situations: Reading a `.so` that was stripped/cut by a buggy packager; downloading a wheel payload that was truncated mid-transfer; test fixtures created by concatenating a real ELF header prefix with junk.
Related errors
- unrecognized capacity ({self.capacity}) or encoding ({self.e
- unable to parse identification
- invalid magic: {magic!r}
- Bad metadata in {dist} (invalid metadata entry 'name')
- Wheel '{name}' located at {location} is invalid.
AI-assisted analysis of pypa/pip@d7d0d0a394 (2026-08-04).
Data as JSON: /data/errors/009b609d48829d39.json.
Report an issue: GitHub.