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 {...@@ -1043,7 +1043,7 @@ pub const Elf32 = struct {
1043 pub const Addr = u32;1043 pub const Addr = u32;
1044 pub const Off = u32;1044 pub const Off = u32;
1045 pub const Ehdr = extern struct {1045 pub const Ehdr = extern struct {
1046 ident: [EI.NIDENT]u8,1046 ident: Ident,
1047 type: ET,1047 type: ET,
1048 machine: EM,1048 machine: EM,
1049 version: Word,1049 version: Word,
...@@ -1133,7 +1133,7 @@ pub const Elf64 = struct {...@@ -1133,7 +1133,7 @@ pub const Elf64 = struct {
1133 pub const Addr = u64;1133 pub const Addr = u64;
1134 pub const Off = u64;1134 pub const Off = u64;
1135 pub const Ehdr = extern struct {1135 pub const Ehdr = extern struct {
1136 ident: [EI.NIDENT]u8,1136 ident: Ident,
1137 type: ET,1137 type: ET,
1138 machine: EM,1138 machine: EM,
1139 version: Word,1139 version: Word,
...@@ -1611,6 +1611,20 @@ pub const Sym = switch (@sizeOf(usize)) {...@@ -1611,6 +1611,20 @@ pub const Sym = switch (@sizeOf(usize)) {
1611/// Deprecated, use `std.elf.ElfN.Addr`1611/// Deprecated, use `std.elf.ElfN.Addr`
1612pub const Addr = ElfN.Addr;1612pub 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
1614/// Deprecated, use `@intFromEnum(std.elf.CLASS.NONE)`1628/// Deprecated, use `@intFromEnum(std.elf.CLASS.NONE)`
1615pub const ELFCLASSNONE = @intFromEnum(CLASS.NONE);1629pub const ELFCLASSNONE = @intFromEnum(CLASS.NONE);
1616/// Deprecated, use `@intFromEnum(std.elf.CLASS.@"32")`1630/// Deprecated, use `@intFromEnum(std.elf.CLASS.@"32")`
lib/std/posix.zig+1-1
...@@ -834,7 +834,7 @@ pub fn dl_iterate_phdr(...@@ -834,7 +834,7 @@ pub fn dl_iterate_phdr(
834 while (it.next()) |entry| {834 while (it.next()) |entry| {
835 const phdrs: []elf.ElfN.Phdr = if (entry.addr != 0) phdrs: {835 const phdrs: []elf.ElfN.Phdr = if (entry.addr != 0) phdrs: {
836 const ehdr: *elf.ElfN.Ehdr = @ptrFromInt(entry.addr);836 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));
838 const phdrs: [*]elf.ElfN.Phdr = @ptrFromInt(entry.addr + ehdr.phoff);838 const phdrs: [*]elf.ElfN.Phdr = @ptrFromInt(entry.addr + ehdr.phoff);
839 break :phdrs phdrs[0..ehdr.phnum];839 break :phdrs phdrs[0..ehdr.phnum];
840 } else getSelfPhdrs();840 } else getSelfPhdrs();
src/link/Elf2.zig+7-8
...@@ -1150,14 +1150,13 @@ fn initHeaders(...@@ -1150,14 +1150,13 @@ fn initHeaders(
1150 elf.nodes.appendAssumeCapacity(.ehdr);1150 elf.nodes.appendAssumeCapacity(.ehdr);
11511151
1152 const ehdr: *ElfN.Ehdr = @ptrCast(@alignCast(elf.ni.ehdr.slice(&elf.mf)));1152 const ehdr: *ElfN.Ehdr = @ptrCast(@alignCast(elf.ni.ehdr.slice(&elf.mf)));
1153 const EI = std.elf.EI;1153 ehdr.ident = .{
1154 @memcpy(ehdr.ident[0..std.elf.MAGIC.len], std.elf.MAGIC);1154 .class = class,
1155 ehdr.ident[EI.CLASS] = @intFromEnum(class);1155 .data = data,
1156 ehdr.ident[EI.DATA] = @intFromEnum(data);1156 .version = 1,
1157 ehdr.ident[EI.VERSION] = 1;1157 .osabi = osabi,
1158 ehdr.ident[EI.OSABI] = @intFromEnum(osabi);1158 .abiversion = 0,
1159 ehdr.ident[EI.ABIVERSION] = 0;1159 };
1160 @memset(ehdr.ident[EI.PAD..], 0);
1161 ehdr.type = @"type";1160 ehdr.type = @"type";
1162 ehdr.machine = machine;1161 ehdr.machine = machine;
1163 ehdr.version = 1;1162 ehdr.version = 1;