pypa/pip · error · ELFInvalid
unable to parse machine and section information
Error message
unable to parse machine and section information
What it means
After validating identification bytes, ELFFile reads the main ELF header (type, machine, entry point, program header offset, flags, etc.) using a format string determined by class/encoding. If the file is truncated before the full header (52 bytes for 32-bit, 64 bytes for 64-bit after the 16-byte ident), struct.unpack raises struct.error, caught and re-raised as ELFInvalid.
Solutions
- Check the file size meets the minimum for the detected class (at least 68 bytes for 32-bit, 80 bytes for 64-bit)
- Re-download or re-extract the file from its source
- Catch ELFInvalid and report the file as corrupted in batch processing
Example fix
# before
elf = ELFFile(open("truncated_binary", "rb")) # raises
# after
import os
file_size = os.path.getsize("truncated_binary")
if file_size < 80:
raise ValueError(f"File is only {file_size} bytes, appears truncated")
elf = ELFFile(open("truncated_binary", "rb")) Defensive patterns
Strategy: try-catch
Validate before calling
import os
def check_min_elf_size(path):
# 16-byte ident + 48-byte header minimum
if os.path.getsize(path) < 64:
raise ValueError("File too small for a complete ELF header") Try / catch
from packaging._elffile import ELFFile, ELFInvalid
try:
elf = ELFFile(f)
except ELFInvalid as e:
if "unable to parse machine" in str(e):
log.error(f"Truncated ELF file: {path}") Prevention
- Verify file integrity with checksums after download
- Check file sizes against expected minimums
- Handle truncated files explicitly in batch processing
When it happens
Trigger: A truncated ELF file: valid magic and identification bytes but cut off before the complete ELF header can be read.
Common situations: Interrupted downloads, partial disk writes, corrupted archive extraction, or deliberately truncated test fixtures that start with a valid ELF magic but are incomplete.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- invalid magic
- unable to parse identification
- unrecognized capacity
- Algorithm used in hash field has different value in hashes…
- Algorithm used in hash field is not present in hashes field
AI-assisted analysis of pypa/pip@f399c37189 (2026-08-08).
Data as JSON: /api/errors/009b609d48829d39.
Report an issue: GitHub.
Appendix: 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 a program header entry.
self._e_phnum, # Number of program headers.
) = 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 f399c37189)