{"record":{"id":"170bf7a23f938547","repo":"nodejs/node","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":"validation","errorClass":"ELFInvalid","httpStatus":null,"severity":"error","filePath":"tools/gyp/pylib/packaging/_elffile.py","lineNumber":87,"sourceCode":"                f\"unrecognized capacity ({self.capacity}) or \"\n                f\"encoding ({self.encoding})\"\n            )\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) -> Optional[str]:\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":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/gyp/pylib/packaging/_elffile.py#L69-L105","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-download or rebuild the ELF file — it is truncated.","Check the file size against the expected minimum for a valid ELF (at least ~52 bytes for 32-bit, ~64 for 64-bit).","Wrap ELFFile construction in try/except ELFInvalid and skip corrupt files.","Verify integrity with readelf -h; it will report 'Error: Unable to read the ELF header'."],"exampleFix":"import os\npath = 'libfoo.so'\nif os.path.getsize(path) < 64:\n    print('file too small to be a complete ELF')\nelse:\n    try:\n        elf = ELFFile(open(path, 'rb'))\n    except ELFInvalid:\n        print('truncated or corrupt ELF')","handlingStrategy":"try-catch","validationCode":"# Ensure the file is large enough for a complete ELF header\nimport os, struct\nmin_header = 52  # 32-bit; 64-bit needs 64\nif os.path.getsize(path) < min_header:\n    raise ValueError(f'{path} is truncated (< {min_header} bytes)')","typeGuard":"def is_complete_elf(path: str) -> bool:\n    import os\n    # 32-bit ELF header = 52 bytes; 64-bit = 64 bytes\n    return os.path.getsize(path) >= 64 and open(path, 'rb').read(4) == b'\\x7fELF'","tryCatchPattern":"from packaging._elffile import ELFFile, ELFInvalid\ntry:\n    elf = ELFFile(open(path, 'rb'))\nexcept ELFInvalid as e:\n    if 'machine and section' in str(e):\n        print('truncated ELF header')\n    raise","preventionTips":["Verify file completeness (size, checksum) before parsing ELF headers.","Re-download or rebuild binaries that fail ELF header validation."],"tags":["elf","binary","parsing","packaging","truncated"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}