{"id":"009b609d48829d39","repo":"pypa/pip","slug":"unable-to-parse-machine-and-section-information","errorCode":null,"errorMessage":"unable to parse machine and section information","messagePattern":"unable to parse machine and section information","errorType":"exception","errorClass":"ELFInvalid","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/packaging/_elffile.py","lineNumber":88,"sourceCode":"            raise ELFInvalid(\n                f\"unrecognized capacity ({self.capacity}) or encoding ({self.encoding})\"\n            ) from e\n\n        try:\n            (\n                _,\n                self.machine,  # Architecture type.\n                _,\n                _,\n                self._e_phoff,  # Offset of program header.\n                _,\n                self.flags,  # Processor-specific flags.\n                _,\n                self._e_phentsize,  # Size of section.\n                self._e_phnum,  # Number of sections.\n            ) = self._read(e_fmt)\n        except struct.error as e:\n            raise ELFInvalid(\"unable to parse machine and section information\") from e\n\n    def _read(self, fmt: str) -> tuple[int, ...]:\n        return struct.unpack(fmt, self._f.read(struct.calcsize(fmt)))\n\n    @property\n    def interpreter(self) -> str | None:\n        \"\"\"\n        The path recorded in the ``PT_INTERP`` section header.\n        \"\"\"\n        for index in range(self._e_phnum):\n            self._f.seek(self._e_phoff + self._e_phentsize * index)\n            try:\n                data = self._read(self._p_fmt)\n            except struct.error:\n                continue\n            if data[self._p_idx[0]] != 3:  # Not PT_INTERP.\n                continue\n            self._f.seek(data[self._p_idx[1]])","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/packaging/_elffile.py#L70-L106","documentation":"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.).","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nelf = ELFFile(f)\n// after\nimport os\nif os.path.getsize(path) < 64:\n    raise ValueError(f'{path} truncated, cannot be a complete ELF')\ntry:\n    elf = ELFFile(f)\nexcept ELFInvalid as e:\n    raise ValueError(f'{path} corrupt ELF: {e}') from e","handlingStrategy":"try-catch","validationCode":"import os\n\ndef is_full_elf_header(path: str) -> bool:\n    # 52 bytes for 32-bit, 64 for 64-bit ELF header\n    return os.path.getsize(path) >= 64","typeGuard":"def is_plausible_elf_size(size: int) -> bool:\n    return isinstance(size, int) and size >= 64","tryCatchPattern":"from packaging._elffile import ELFFile, ELFInvalid\ntry:\n    elf = ELFFile(f)\nexcept ELFInvalid as e:\n    if 'machine and section' in str(e):\n        log.warning('%s has truncated ELF header', path)\n        continue\n    raise","preventionTips":["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"],"tags":["elf","packaging","binary-parsing","corruption"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}