{"record":{"id":"e28cad677a9768c8","repo":"jdx/mise","slug":"unexpected-elf-e-phentsize-e-phentsize","errorCode":null,"errorMessage":"unexpected ELF e_phentsize {e_phentsize}","messagePattern":"unexpected ELF e_phentsize (.+?)","errorType":"exception","errorClass":"eyre::Report","httpStatus":null,"severity":"error","filePath":"src/system/packages/brew/elf.rs","lineNumber":114,"sourceCode":"    b[off..off + 8].copy_from_slice(&v.to_le_bytes());\n}\n\n#[derive(Clone, Copy)]\nstruct Phdr {\n    p_type: u32,\n    p_offset: u64,\n    p_vaddr: u64,\n    p_filesz: u64,\n    p_memsz: u64,\n    p_align: u64,\n}\n\nfn read_phdrs(content: &[u8]) -> Result<Vec<Phdr>> {\n    let e_phoff = rd_u64(content, 32)? as usize;\n    let e_phentsize = rd_u16(content, 54)? as usize;\n    let e_phnum = rd_u16(content, 56)? as usize;\n    if e_phentsize != PHDR_SIZE {\n        bail!(\"unexpected ELF e_phentsize {e_phentsize}\");\n    }\n    if e_phnum >= 0xffff {\n        bail!(\"ELF uses PN_XNUM program header counts\");\n    }\n    let mut phdrs = Vec::with_capacity(e_phnum);\n    for i in 0..e_phnum {\n        let off = e_phoff + i * PHDR_SIZE;\n        phdrs.push(Phdr {\n            p_type: rd_u32(content, off)?,\n            p_offset: rd_u64(content, off + 8)?,\n            p_vaddr: rd_u64(content, off + 16)?,\n            p_filesz: rd_u64(content, off + 32)?,\n            p_memsz: rd_u64(content, off + 40)?,\n            p_align: rd_u64(content, off + 48)?,\n        });\n    }\n    Ok(phdrs)\n}","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/jdx/mise/blob/9dcfcaa0dc8747a2577d3270b69bb9d8313b2807/src/system/packages/brew/elf.rs#L96-L132","documentation":"While pouring a Linux bottle, the ELF rewriter (elf::patch) walks the program header table and requires e_phentsize to equal 56 (sizeof Elf64_Phdr for 64-bit). Any other value means the file is corrupt, truncated, or produced by nonstandard tooling, and the parser refuses to continue rather than compute bogus offsets.","triggerScenarios":"elf::patch -> read_phdrs after the 64-bit LE checks pass: rd_u16(content, 54) != PHDR_SIZE (56). Triggered by a truncated/corrupted bottle download, a file with forged ELF magic, or an ELF built by a linker emitting nonstandard program header entry sizes.","commonSituations":"Broken bottle cache after an interrupted download; upstream bottle built with experimental tooling; disk corruption; a text file accidentally named as the binary.","solutions":["Clear the cached bottle and re-download it, verifying the sha256 matches the API manifest","Inspect the file with readelf -h <file> — if e_phentsize is not 56 outside this tool, the binary itself is broken","If readelf shows a valid file, report the binary to the project — the parser may need extending for this producer"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Cheap pre-check before relocation: sane ELF64 header fields.\nfn elf_phentsize_ok(content: &[u8]) -> bool {\n    content.len() >= 56\n        && content[..4] == [0x7f, b'E', b'L', b'F']\n        && content[4] == 2 && content[5] == 1\n        && u16::from_le_bytes(content[54..56].try_into().unwrap()) == 56\n}","typeGuard":"fn is_wellformed_elf64(content: &[u8]) -> bool {\n    content.len() >= 64 && content[..4] == [0x7f, b'E', b'L', b'F']\n        && content[4] == 2 && content[5] == 1\n        && u16::from_le_bytes(content[54..56].try_into().unwrap()) == 56\n        && u16::from_le_bytes(content[56..58].try_into().unwrap()) < 0xffff\n}","tryCatchPattern":"// Treat per-file patch failures as fatal for that formula but report the path:\nmatch elf::patch(&mut content, &opts, &path) {\n    Ok(changed) => { /* ... */ }\n    Err(err) => return Err(err.wrap_err_with(|| format!(\"relocating {}\", path.display()))),\n}","preventionTips":["Verify bottle checksums before extraction so truncated ELFs never reach the rewriter","Run readelf -h on suspicious bottles; e_phentsize != 56 means the file, not the tool, is wrong","Keep pour caches clean — re-extract from a verified download on any ELF parse failure"],"tags":["brew","elf","linux","binary-patching","validation"],"backgroundTag":"elf-header-validation-failed","analyzedSha":"9dcfcaa0dc8747a2577d3270b69bb9d8313b2807","analyzedAt":"2026-08-17T14:28:50.624Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}