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.
This commit is contained in:
Isaac Freund 2025-07-09 19:03:16 +02:00 committed by Alex Rønne Petersen
parent 5360968e03
commit f551c7c581

View File

@ -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) {