{"record":{"id":"35307a45431911fb","repo":"oven-sh/bun","slug":"invalidelffile","errorCode":"InvalidElfFile","errorMessage":"InvalidElfFile","messagePattern":"InvalidElfFile","errorType":"error_code","errorClass":"ElfError","httpStatus":null,"severity":"error","filePath":"src/exe_format/elf.rs","lineNumber":23,"sourceCode":"//! and expands it to hold the standalone module graph data.\n//!\n//! Must work on any host platform (macOS, Windows, Linux) for cross-compilation.\n\nuse core::mem::size_of;\n#[cfg(any(target_os = \"linux\", target_os = \"android\"))]\nuse core::sync::atomic::{AtomicU8, Ordering};\n\n#[cfg(any(target_os = \"linux\", target_os = \"android\"))]\nuse bun_core::env_var;\nuse bun_core::{slice_to_nul, strings};\n\nuse crate::{align_up, read_struct, write_struct};\n\nbun_core::declare_scope!(elf, visible);\n\n#[derive(Debug, thiserror::Error, strum::IntoStaticStr)]\npub enum ElfError {\n    #[error(\"InvalidElfFile\")]\n    InvalidElfFile,\n    #[error(\"Not64Bit\")]\n    Not64Bit,\n    #[error(\"NotLittleEndian\")]\n    NotLittleEndian,\n    #[error(\"BunSectionNotFound\")]\n    BunSectionNotFound,\n    #[error(\"NoWritableLoadSegment\")]\n    NoWritableLoadSegment,\n    #[error(\"NewVaddrCollides\")]\n    NewVaddrCollides,\n}\n\npub struct ElfFile {\n    pub data: Vec<u8>,\n}\n\nimpl ElfFile {","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/oven-sh/bun/blob/8c5296ac459e8252d3cd702f3fbcbb0c249d95d5/src/exe_format/elf.rs#L5-L41","documentation":"ElfError::InvalidElfFile comes from Bun's own ELF writer (src/exe_format/elf.rs) used by `bun build --compile` (via StandaloneModuleGraph) when appending the embedded module graph to a Linux executable. It fires when the input binary is shorter than an ELF64 header, lacks the \\x7fELF magic, or has section-header/shstrtab/RW-segment offsets beyond end-of-file — i.e. the file is not a valid ELF64 or is truncated/corrupted.","triggerScenarios":"Compiling a standalone executable where the ELF template is a Mach-O/PE binary (wrong --target for the host file), a partially copied/truncated bun binary, or a corrupted build artifact with out-of-bounds e_shoff/shstrtab offsets.","commonSituations":"Cross-compiling with a mismatched --target triple; a base executable corrupted in transfer or by post-processing tools (upx, signers, buggy strip); disk-full during a previous build leaving a truncated binary.","solutions":["Verify the base binary with `file ./bun` (must say ELF 64-bit LSB) and rebuild/redownload it if truncated.","Make sure the --compile target triple matches the binary's actual format (use a linux target for ELF patching).","Re-run the failing build after removing corrupted artifacts; check dmesg/disk if truncation repeats."],"exampleFix":"# before: host binary is not the ELF being patched\nbun build --compile --target=darwin-arm64 ./app.ts  # then patching path given an ELF\n\n# after: match formats\nfile ./build/debug/bun-debug  # expect: ELF 64-bit LSB ...\nbun build --compile --target=linux-arm64 ./app.ts","handlingStrategy":"try-catch","validationCode":"fn is_elf64_le(data: &[u8]) -> bool {\n    data.len() >= size_of::<Elf64_Ehdr>() && &data[0..4] == b\"\\x7fELF\"\n}\n// before patching:\nif !is_elf64_le(&template_bytes) {\n    return Err(format!(\"template is not ELF64: {}\", template_path.display()));\n}","typeGuard":"fn is_elf64_le(data: &[u8]) -> bool {\n    data.len() >= 64 && data[..4] == *b\"\\x7fELF\" && data[4] == 2 /* ELFCLASS64 */ && data[5] == 1 /* ELFDATA2LSB */\n}","tryCatchPattern":"match bun_elf::Elf64::from_bytes(template) {\n    Ok(mut elf) => elf.add_section(payload)?,\n    Err(ElfError::InvalidElfFile) => {\n        return Err(format!(\"not a valid ELF64 binary (truncated or wrong format): {}\", path.display()));\n    }\n    Err(ElfError::Not64Bit | ElfError::NotLittleEndian) => return Err(\"wrong ELF class or endianness\".into()),\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Validate the \\x7fELF magic and header size before handing a binary to the ELF patcher.","Match --compile --target to the real architecture of the base executable; never feed Mach-O/PE files to the ELF path.","Treat truncated base binaries (interrupted downloads, disk-full builds) as poisoned — rebuild rather than retry."],"tags":["exe-format","elf","compile","standalone","binary"],"backgroundTag":null,"analyzedSha":"8c5296ac459e8252d3cd702f3fbcbb0c249d95d5","analyzedAt":"2026-08-16T08:01:58.794Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}