authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-05-07 10:59:26+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-05-17 18:08:57+01:00
log55cdf6c28ba80dec9b595ff6e735090d1c081b26
tree81689aec0a6159675772fedc64a13fd91cc9bcbd
parentbdfbf432dd626b495cee0a8aa53e77b4ef718ff2
signaturelock-open Commit is signed but in an unrecognized format.

std.elf: add `Ident` struct

This is much nicer than indexing a raw 16-byte buffer.

3 files changed, 24 insertions(+), 11 deletions(-)

lib/std/elf.zig+16-2
......@@ -1043,7 +1043,7 @@ pub const Elf32 = struct {
10431043 pub const Addr = u32;
10441044 pub const Off = u32;
10451045 pub const Ehdr = extern struct {
1046 ident: [EI.NIDENT]u8,
1046 ident: Ident,
10471047 type: ET,
10481048 machine: EM,
10491049 version: Word,
......@@ -1133,7 +1133,7 @@ pub const Elf64 = struct {
11331133 pub const Addr = u64;
11341134 pub const Off = u64;
11351135 pub const Ehdr = extern struct {
1136 ident: [EI.NIDENT]u8,
1136 ident: Ident,
11371137 type: ET,
11381138 machine: EM,
11391139 version: Word,
......@@ -1611,6 +1611,20 @@ pub const Sym = switch (@sizeOf(usize)) {
16111611/// Deprecated, use `std.elf.ElfN.Addr`
16121612pub const Addr = ElfN.Addr;
16131613
1614pub const Ident = extern struct {
1615 magic: [MAGIC.len]u8 = MAGIC.*,
1616 class: CLASS,
1617 data: DATA,
1618 version: u8,
1619 osabi: OSABI,
1620 abiversion: u8,
1621 pad: [7]u8 = @splat(0),
1622
1623 comptime {
1624 assert(@sizeOf(Ident) == EI.NIDENT);
1625 }
1626};
1627
16141628/// Deprecated, use `@intFromEnum(std.elf.CLASS.NONE)`
16151629pub const ELFCLASSNONE = @intFromEnum(CLASS.NONE);
16161630/// Deprecated, use `@intFromEnum(std.elf.CLASS.@"32")`
lib/std/posix.zig+1-1
......@@ -834,7 +834,7 @@ pub fn dl_iterate_phdr(
834834 while (it.next()) |entry| {
835835 const phdrs: []elf.ElfN.Phdr = if (entry.addr != 0) phdrs: {
836836 const ehdr: *elf.ElfN.Ehdr = @ptrFromInt(entry.addr);
837 assert(mem.eql(u8, ehdr.ident[0..4], elf.MAGIC));
837 assert(mem.eql(u8, &ehdr.ident.magic, elf.MAGIC));
838838 const phdrs: [*]elf.ElfN.Phdr = @ptrFromInt(entry.addr + ehdr.phoff);
839839 break :phdrs phdrs[0..ehdr.phnum];
840840 } else getSelfPhdrs();
src/link/Elf2.zig+7-8
......@@ -1150,14 +1150,13 @@ fn initHeaders(
11501150 elf.nodes.appendAssumeCapacity(.ehdr);
11511151
11521152 const ehdr: *ElfN.Ehdr = @ptrCast(@alignCast(elf.ni.ehdr.slice(&elf.mf)));
1153 const EI = std.elf.EI;
1154 @memcpy(ehdr.ident[0..std.elf.MAGIC.len], std.elf.MAGIC);
1155 ehdr.ident[EI.CLASS] = @intFromEnum(class);
1156 ehdr.ident[EI.DATA] = @intFromEnum(data);
1157 ehdr.ident[EI.VERSION] = 1;
1158 ehdr.ident[EI.OSABI] = @intFromEnum(osabi);
1159 ehdr.ident[EI.ABIVERSION] = 0;
1160 @memset(ehdr.ident[EI.PAD..], 0);
1153 ehdr.ident = .{
1154 .class = class,
1155 .data = data,
1156 .version = 1,
1157 .osabi = osabi,
1158 .abiversion = 0,
1159 };
11611160 ehdr.type = @"type";
11621161 ehdr.machine = machine;
11631162 ehdr.version = 1;