Commit f551c7c581

Isaac Freund <mail@isaacfreund.com>
2025-07-09 19:03:16
link.Elf: check files in archives for ELF magic
Apparently raw LLVM IR Bitcode files ("Bitstreams") may appear in archives with LTO enabled. I observed this in the wild on Chimera Linux. I'm not yet sure if it's in scope for Zig to support these special archives, but we should at least give a correct error message.
1 parent 5360968
Changed files (1)
src
link
src/link/Elf/Object.zig
@@ -106,6 +106,9 @@ pub fn parseCommon(
     const header_buffer = try Elf.preadAllAlloc(gpa, handle, offset, @sizeOf(elf.Elf64_Ehdr));
     defer gpa.free(header_buffer);
     self.header = @as(*align(1) const elf.Elf64_Ehdr, @ptrCast(header_buffer)).*;
+    if (!mem.eql(u8, self.header.?.e_ident[0..4], elf.MAGIC)) {
+        return diags.failParse(path, "not an ELF file", .{});
+    }
 
     const em = target.toElfMachine();
     if (em != self.header.?.e_machine) {