nodejs/node · error · ELFInvalid
invalid magic: {magic!r}
Error message
invalid magic: {magic!r} What it means
Raised by ELFFile.__init__ when the first 4 bytes of the file are not the ELF magic bytes b'\x7fELF'. The identification field was read successfully (at least 16 bytes) but the magic signature does not match, meaning the file is well-formed enough to read but is not an ELF executable.
Source
Thrown at tools/gyp/pylib/packaging/_elffile.py:52
Arm = 40
X8664 = 62
AArc64 = 183
class ELFFile:
"""
Representation of an ELF executable.
"""
def __init__(self, f: IO[bytes]) -> None:
self._f = f
try:
ident = self._read("16B")
except struct.error:
raise ELFInvalid("unable to parse identification")
if (magic := bytes(ident[:4])) != b"\x7fELF":
raise ELFInvalid(f"invalid magic: {magic!r}")
self.capacity = ident[4] # Format for program header (bitness).
self.encoding = ident[5] # Data structure encoding (endianness).
try:
# e_fmt: Format for program header.
# p_fmt: Format for section header.
# p_idx: Indexes to find p_type, p_offset, and p_filesz.
e_fmt, self._p_fmt, self._p_idx = {
(1, 1): ("<HHIIIIIHHH", "<IIIIIIII", (0, 1, 4)), # 32-bit LSB.
(1, 2): (">HHIIIIIHHH", ">IIIIIIII", (0, 1, 4)), # 32-bit MSB.
(2, 1): ("<HHIQQQIHHH", "<IIQQQQQQ", (0, 2, 5)), # 64-bit LSB.
(2, 2): (">HHIQQQIHHH", ">IIQQQQQQ", (0, 2, 5)), # 64-bit MSB.
}[(self.capacity, self.encoding)]
except KeyError:
raise ELFInvalid(
f"unrecognized capacity ({self.capacity}) or "
f"encoding ({self.encoding})"View on GitHub (pinned to 1b2de5e052)
Solutions
- Confirm the file is an ELF binary (Linux/BSD executable or shared object) before parsing.
- Check the file with the `file` command: it should report 'ELF'.
- Catch ELFInvalid and handle non-ELF files as a normal case (return None or skip).
- If you expected an ELF file, the file may have been corrupted or replaced — re-fetch it.
Example fix
# before
elf = ELFFile(open('app.exe', 'rb')) # 'MZ' magic -> raises
# after
try:
elf = ELFFile(open(path, 'rb'))
except ELFInvalid as e:
if 'magic' in str(e):
print('not an ELF binary; skipping') Defensive patterns
Strategy: type-guard
Validate before calling
# Check magic bytes before constructing ELFFile
with open(path, 'rb') as f:
if f.read(4) != b'\x7fELF':
raise ValueError(f'{path} is not an ELF binary')
f.seek(0) Type guard
def is_elf_file(path: str) -> bool:
try:
with open(path, 'rb') as f:
return f.read(4) == b'\x7fELF'
except OSError:
return False Try / catch
from packaging._elffile import ELFFile, ELFInvalid
try:
elf = ELFFile(open(path, 'rb'))
except ELFInvalid as e:
if 'magic' in str(e):
print('not an ELF file')
raise Prevention
- Verify the file format with `file <path>` before ELF parsing.
- Use a magic-byte type guard when scanning directories that may contain mixed binary formats.
When it happens
Trigger: ELFFile(f) reads 16 bytes successfully, but bytes(ident[:4]) != b'\x7fELF'. This happens with any non-ELF binary: a PE/COFF Windows exe (starts with 'MZ'), a Mach-O file (starts with 0xFEEDFACE/0xFEEDFAT), an ar archive, a plain object file in another format, or a text file at least 16 bytes long.
Common situations: Using ELFFile to inspect a wheel/platform tag on a file that is a Windows .exe or .dll, a macOS .dylib, or a static .a archive. Pointing at a non-binary file.
Related errors
- unable to parse identification
- unrecognized capacity ({self.capacity}) or encoding ({self.e
- unable to parse machine and section information
- duplicate labels in project urls
- unknown compiler: %s
AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13).
Data as JSON: /api/errors/ded2a0e8085cf87e.
Report an issue: GitHub.