authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-09-06 23:16:58+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-09-07 01:29:49+02:00
log85953a78a34c862a22dbb37f1fc5755ba305d7e4
treea25cb2c5515f51a1c1fe3ab4878762a7bec132ed
parent8839d95005bff0742ba9cd75b069b4967c35c04c
signature Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.elf: add AT_* constants for more OSs and namespace them properly


9 files changed, 244 insertions(+), 66 deletions(-)

lib/std/elf.zig+223-45
...@@ -1,56 +1,234 @@...@@ -1,56 +1,234 @@
1//! Executable and Linkable Format.1//! Executable and Linkable Format.
22
3const builtin = @import("builtin");
3const std = @import("std.zig");4const std = @import("std.zig");
4const Io = std.Io;5const Io = std.Io;
5const math = std.math;6const math = std.math;
6const mem = std.mem;7const mem = std.mem;
7const assert = std.debug.assert;8const assert = std.debug.assert;
8const Endian = std.builtin.Endian;9const Endian = std.builtin.Endian;
9const native_endian = @import("builtin").target.cpu.arch.endian();10
1011const native_endian = builtin.target.cpu.arch.endian();
11pub const AT_NULL = 0;12const native_os = builtin.target.os.tag;
12pub const AT_IGNORE = 1;13
13pub const AT_EXECFD = 2;14pub const AT = switch (native_os) {
14pub const AT_PHDR = 3;15 .dragonfly => struct {
15pub const AT_PHENT = 4;16 pub const NULL = 0;
16pub const AT_PHNUM = 5;17 pub const IGNORE = 1;
17pub const AT_PAGESZ = 6;18 pub const EXECFD = 2;
18pub const AT_BASE = 7;19 pub const PHDR = 3;
19pub const AT_FLAGS = 8;20 pub const PHENT = 4;
20pub const AT_ENTRY = 9;21 pub const PHNUM = 5;
21pub const AT_NOTELF = 10;22 pub const PAGESZ = 6;
22pub const AT_UID = 11;23 pub const BASE = 7;
23pub const AT_EUID = 12;24 pub const FLAGS = 8;
24pub const AT_GID = 13;25 pub const ENTRY = 9;
25pub const AT_EGID = 14;26 pub const NOTELF = 10;
26pub const AT_CLKTCK = 17;27 pub const UID = 11;
27pub const AT_PLATFORM = 15;28 pub const EUID = 12;
28pub const AT_HWCAP = 16;29 pub const GID = 13;
29pub const AT_FPUCW = 18;30 pub const EGID = 14;
30pub const AT_DCACHEBSIZE = 19;31 pub const EXECPATH = 15;
31pub const AT_ICACHEBSIZE = 20;32 pub const CANARY = 16;
32pub const AT_UCACHEBSIZE = 21;33 pub const CANARYLEN = 17;
33pub const AT_IGNOREPPC = 22;34 pub const OSRELDATE = 18;
34pub const AT_SECURE = 23;35 pub const NCPUS = 19;
35pub const AT_BASE_PLATFORM = 24;36 pub const PAGESIZES = 20;
36pub const AT_RANDOM = 25;37 pub const PAGESIZESLEN = 21;
37pub const AT_HWCAP2 = 26;38 pub const STACKPROT = 23;
38pub const AT_EXECFN = 31;39 },
39pub const AT_SYSINFO = 32;40 .freebsd => struct {
40pub const AT_SYSINFO_EHDR = 33;41 pub const NULL = 0;
41pub const AT_L1I_CACHESHAPE = 34;42 pub const IGNORE = 1;
42pub const AT_L1D_CACHESHAPE = 35;43 pub const EXECFD = 2;
43pub const AT_L2_CACHESHAPE = 36;44 pub const PHDR = 3;
44pub const AT_L3_CACHESHAPE = 37;45 pub const PHENT = 4;
45pub const AT_L1I_CACHESIZE = 40;46 pub const PHNUM = 5;
46pub const AT_L1I_CACHEGEOMETRY = 41;47 pub const PAGESZ = 6;
47pub const AT_L1D_CACHESIZE = 42;48 pub const BASE = 7;
48pub const AT_L1D_CACHEGEOMETRY = 43;49 pub const FLAGS = 8;
49pub const AT_L2_CACHESIZE = 44;50 pub const ENTRY = 9;
50pub const AT_L2_CACHEGEOMETRY = 45;51 pub const NOTELF = 10;
51pub const AT_L3_CACHESIZE = 46;52 pub const UID = 11;
52pub const AT_L3_CACHEGEOMETRY = 47;53 pub const EUID = 12;
53pub const AT_MINSIGSTKSZ = 51;54 pub const GID = 13;
55 pub const EGID = 14;
56 pub const EXECPATH = 15;
57 pub const CANARY = 16;
58 pub const CANARYLEN = 17;
59 pub const OSRELDATE = 18;
60 pub const NCPUS = 19;
61 pub const PAGESIZES = 20;
62 pub const PAGESIZESLEN = 21;
63 pub const TIMEKEEP = 22;
64 pub const STACKPROT = 23;
65 pub const EHDRFLAGS = 24;
66 pub const HWCAP = 25;
67 pub const HWCAP2 = 26;
68 pub const BSDFLAGS = 27;
69 pub const ARGC = 28;
70 pub const ARGV = 29;
71 pub const ENVC = 30;
72 pub const ENVV = 31;
73 pub const PS_STRINGS = 32;
74 pub const FXRNG = 33;
75 pub const KPRELOAD = 34;
76 pub const USRSTACKBASE = 35;
77 pub const USRSTACKLIM = 36;
78 pub const CHERI_STATS = 37;
79 pub const HWCAP3 = 38;
80 pub const HWCAP4 = 39;
81 },
82 .illumos => struct {
83 pub const NULL = 0;
84 pub const IGNORE = 1;
85 pub const EXECFD = 2;
86 pub const PHDR = 3;
87 pub const PHENT = 4;
88 pub const PHNUM = 5;
89 pub const PAGESZ = 6;
90 pub const BASE = 7;
91 pub const FLAGS = 8;
92 pub const ENTRY = 9;
93 pub const NOTELF = 10;
94 pub const UID = 11;
95 pub const EUID = 12;
96 pub const GID = 13;
97 pub const EGID = 14;
98 pub const EXECPATH = 15;
99 pub const CANARY = 16;
100 pub const CANARYLEN = 17;
101 pub const OSRELDATE = 18;
102 pub const NCPUS = 19;
103 pub const PAGESIZES = 20;
104 pub const PAGESIZESLEN = 21;
105 pub const TIMEKEEP = 22;
106 pub const STACKPROT = 23;
107 },
108 .linux => struct {
109 pub const NULL = 0;
110 pub const IGNORE = 1;
111 pub const EXECFD = 2;
112 pub const PHDR = 3;
113 pub const PHENT = 4;
114 pub const PHNUM = 5;
115 pub const PAGESZ = 6;
116 pub const BASE = 7;
117 pub const FLAGS = 8;
118 pub const ENTRY = 9;
119 pub const NOTELF = 10;
120 pub const UID = 11;
121 pub const EUID = 12;
122 pub const GID = 13;
123 pub const EGID = 14;
124 pub const CLKTCK = 17;
125 pub const PLATFORM = 15;
126 pub const HWCAP = 16;
127 pub const FPUCW = 18;
128 pub const DCACHEBSIZE = 19;
129 pub const ICACHEBSIZE = 20;
130 pub const UCACHEBSIZE = 21;
131 pub const IGNOREPPC = 22;
132 pub const SECURE = 23;
133 pub const BASE_PLATFORM = 24;
134 pub const RANDOM = 25;
135 pub const HWCAP2 = 26;
136 pub const EXECFN = 31;
137 pub const SYSINFO = 32;
138 pub const SYSINFO_EHDR = 33;
139 pub const L1I_CACHESHAPE = 34;
140 pub const L1D_CACHESHAPE = 35;
141 pub const L2_CACHESHAPE = 36;
142 pub const L3_CACHESHAPE = 37;
143 pub const L1I_CACHESIZE = 40;
144 pub const L1I_CACHEGEOMETRY = 41;
145 pub const L1D_CACHESIZE = 42;
146 pub const L1D_CACHEGEOMETRY = 43;
147 pub const L2_CACHESIZE = 44;
148 pub const L2_CACHEGEOMETRY = 45;
149 pub const L3_CACHESIZE = 46;
150 pub const L3_CACHEGEOMETRY = 47;
151 pub const MINSIGSTKSZ = 51;
152 },
153 .netbsd => struct {
154 pub const NULL = 0;
155 pub const IGNORE = 1;
156 pub const EXECFD = 2;
157 pub const PHDR = 3;
158 pub const PHENT = 4;
159 pub const PHNUM = 5;
160 pub const PAGESZ = 6;
161 pub const BASE = 7;
162 pub const FLAGS = 8;
163 pub const ENTRY = 9;
164 pub const MIPS_NOTELF = 10;
165 pub const DCACHEBSIZE = 10;
166 pub const ICACHEBSIZE = 11;
167 pub const UCACHEBSIZE = 12;
168 pub const STACKBASE = 13;
169 pub const EUID = 2000;
170 pub const RUID = 2001;
171 pub const EGID = 2002;
172 pub const RGID = 2003;
173 pub const SUN_LDELF = 2004;
174 pub const SUN_LDSHDR = 2005;
175 pub const SUN_LDNAME = 2006;
176 pub const SUN_LPGSIZE = 2007;
177 pub const SUN_PLATFORM = 2008;
178 pub const SUN_HWCAP = 2009;
179 pub const SUN_IFLUSH = 2010;
180 pub const SUN_CPU = 2011;
181 pub const SUN_EMUL_ENTRY = 2012;
182 pub const SUN_EMUL_EXECFD = 2013;
183 pub const SUN_EXECNAME = 2014;
184 },
185 .openbsd => struct {
186 pub const NULL = 0;
187 pub const IGNORE = 1;
188 pub const PAGESZ = 6;
189 pub const HWCAP = 25;
190 pub const HWCAP2 = 26;
191 },
192 .serenity => struct {
193 pub const NULL = 0;
194 pub const IGNORE = 1;
195 pub const EXECFD = 2;
196 pub const PHDR = 3;
197 pub const PHENT = 4;
198 pub const PHNUM = 5;
199 pub const PAGESZ = 6;
200 pub const BASE = 7;
201 pub const FLAGS = 8;
202 pub const ENTRY = 9;
203 pub const NOTELF = 10;
204 pub const UID = 11;
205 pub const EUID = 12;
206 pub const GID = 13;
207 pub const PLATFORM = 15;
208 pub const HWCAP = 16;
209 pub const CLKTCK = 17;
210 pub const SECURE = 23;
211 pub const BASE_PLATFORM = 24;
212 pub const RANDOM = 25;
213 pub const HWCAP2 = 26;
214 pub const EXECFN = 31;
215 pub const EXE_BASE = 32;
216 pub const EXE_SIZE = 33;
217 },
218 else => struct {
219 pub const NULL = 0;
220 pub const IGNORE = 1;
221 pub const EXECFD = 2;
222 pub const PHDR = 3;
223 pub const PHENT = 4;
224 pub const PHNUM = 5;
225 pub const PAGESZ = 6;
226 pub const BASE = 7;
227 pub const FLAGS = 8;
228 pub const ENTRY = 9;
229 pub const NOTELF = 10;
230 },
231};
54232
55pub const DT_NULL = 0;233pub const DT_NULL = 0;
56pub const DT_NEEDED = 1;234pub const DT_NEEDED = 1;
lib/std/heap.zig+1-1
...@@ -80,7 +80,7 @@ pub fn defaultQueryPageSize() usize {...@@ -80,7 +80,7 @@ pub fn defaultQueryPageSize() usize {
80 .linux => if (builtin.link_libc)80 .linux => if (builtin.link_libc)
81 @max(std.c.sysconf(@backingInt(std.c._SC.PAGESIZE)), 0)81 @max(std.c.sysconf(@backingInt(std.c._SC.PAGESIZE)), 0)
82 else82 else
83 std.os.linux.getauxval(std.elf.AT_PAGESZ),83 std.os.linux.getauxval(std.elf.AT.PAGESZ),
84 .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => {84 .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => {
85 const task_port = std.c.mach_task_self();85 const task_port = std.c.mach_task_self();
86 // mach_task_self may fail "if there are any resource failures or other errors".86 // mach_task_self may fail "if there are any resource failures or other errors".
lib/std/os/linux.zig+1-1
...@@ -705,7 +705,7 @@ fn getauxvalImpl(index: usize) callconv(.c) usize {...@@ -705,7 +705,7 @@ fn getauxvalImpl(index: usize) callconv(.c) usize {
705 @disableInstrumentation();705 @disableInstrumentation();
706 const auxv = elf_aux_maybe orelse return 0;706 const auxv = elf_aux_maybe orelse return 0;
707 var i: usize = 0;707 var i: usize = 0;
708 while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {708 while (auxv[i].a_type != std.elf.AT.NULL) : (i += 1) {
709 if (auxv[i].a_type == index)709 if (auxv[i].a_type == index)
710 return auxv[i].a_un.a_val;710 return auxv[i].a_un.a_val;
711 }711 }
lib/std/os/linux/test.zig+4-4
...@@ -106,10 +106,10 @@ test "statx" {...@@ -106,10 +106,10 @@ test "statx" {
106106
107test "user and group ids" {107test "user and group ids" {
108 if (builtin.link_libc) return error.SkipZigTest;108 if (builtin.link_libc) return error.SkipZigTest;
109 try expectEqual(linux.getauxval(elf.AT_UID), linux.getuid());109 try expectEqual(linux.getauxval(elf.AT.UID), linux.getuid());
110 try expectEqual(linux.getauxval(elf.AT_GID), linux.getgid());110 try expectEqual(linux.getauxval(elf.AT.GID), linux.getgid());
111 try expectEqual(linux.getauxval(elf.AT_EUID), linux.geteuid());111 try expectEqual(linux.getauxval(elf.AT.EUID), linux.geteuid());
112 try expectEqual(linux.getauxval(elf.AT_EGID), linux.getegid());112 try expectEqual(linux.getauxval(elf.AT.EGID), linux.getegid());
113}113}
114114
115test "fadvise" {115test "fadvise" {
lib/std/os/linux/vdso.zig+1-1
...@@ -5,7 +5,7 @@ const mem = std.mem;...@@ -5,7 +5,7 @@ const mem = std.mem;
5const maxInt = std.math.maxInt;5const maxInt = std.math.maxInt;
66
7pub fn lookup(vername: []const u8, name: []const u8) usize {7pub fn lookup(vername: []const u8, name: []const u8) usize {
8 const vdso_addr = linux.getauxval(std.elf.AT_SYSINFO_EHDR);8 const vdso_addr = linux.getauxval(std.elf.AT.SYSINFO_EHDR);
9 if (vdso_addr == 0) return 0;9 if (vdso_addr == 0) return 0;
1010
11 const eh = @as(*elf.Ehdr, @ptrFromInt(vdso_addr));11 const eh = @as(*elf.Ehdr, @ptrFromInt(vdso_addr));
lib/std/posix.zig+4-4
...@@ -775,9 +775,9 @@ pub fn sysctl(...@@ -775,9 +775,9 @@ pub fn sysctl(
775775
776pub fn getSelfPhdrs() []std.elf.ElfN.Phdr {776pub fn getSelfPhdrs() []std.elf.ElfN.Phdr {
777 const getauxval = if (builtin.link_libc) std.c.getauxval else std.os.linux.getauxval;777 const getauxval = if (builtin.link_libc) std.c.getauxval else std.os.linux.getauxval;
778 assert(getauxval(std.elf.AT_PHENT) == @sizeOf(std.elf.ElfN.Phdr));778 assert(getauxval(std.elf.AT.PHENT) == @sizeOf(std.elf.ElfN.Phdr));
779 const phdrs: [*]std.elf.ElfN.Phdr = @ptrFromInt(getauxval(std.elf.AT_PHDR));779 const phdrs: [*]std.elf.ElfN.Phdr = @ptrFromInt(getauxval(std.elf.AT.PHDR));
780 return phdrs[0..getauxval(std.elf.AT_PHNUM)];780 return phdrs[0..getauxval(std.elf.AT.PHNUM)];
781}781}
782782
783pub fn dl_iterate_phdr(783pub fn dl_iterate_phdr(
...@@ -819,7 +819,7 @@ pub fn dl_iterate_phdr(...@@ -819,7 +819,7 @@ pub fn dl_iterate_phdr(
819 .PHDR => break @intFromPtr(phdrs.ptr) - phdr.vaddr,819 .PHDR => break @intFromPtr(phdrs.ptr) - phdr.vaddr,
820 else => {},820 else => {},
821 } else unreachable,821 } else unreachable,
822 .name = switch (getauxval(std.elf.AT_EXECFN)) {822 .name = switch (getauxval(std.elf.AT.EXECFN)) {
823 0 => "/proc/self/exe",823 0 => "/proc/self/exe",
824 else => |name| @ptrFromInt(name),824 else => |name| @ptrFromInt(name),
825 },825 },
lib/std/start.zig+6-6
...@@ -617,11 +617,11 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {...@@ -617,11 +617,11 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {
617 var i: usize = 0;617 var i: usize = 0;
618 var at_phdr: usize = 0;618 var at_phdr: usize = 0;
619 var at_phnum: usize = 0;619 var at_phnum: usize = 0;
620 while (auxv[i].a_type != elf.AT_NULL) : (i += 1) {620 while (auxv[i].a_type != elf.AT.NULL) : (i += 1) {
621 switch (auxv[i].a_type) {621 switch (auxv[i].a_type) {
622 elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val,622 elf.AT.PHNUM => at_phnum = auxv[i].a_un.a_val,
623 elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val,623 elf.AT.PHDR => at_phdr = auxv[i].a_un.a_val,
624 elf.AT_HWCAP => at_hwcap = auxv[i].a_un.a_val,624 elf.AT.HWCAP => at_hwcap = auxv[i].a_un.a_val,
625 else => continue,625 else => continue,
626 }626 }
627 }627 }
...@@ -736,8 +736,8 @@ fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) cal...@@ -736,8 +736,8 @@ fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) cal
736736
737 switch (builtin.os.tag) {737 switch (builtin.os.tag) {
738 .linux => {738 .linux => {
739 const at_phdr = std.c.getauxval(elf.AT_PHDR);739 const at_phdr = std.c.getauxval(elf.AT.PHDR);
740 const at_phnum = std.c.getauxval(elf.AT_PHNUM);740 const at_phnum = std.c.getauxval(elf.AT.PHNUM);
741 const phdrs = (@as([*]elf.ElfN.Phdr, @ptrFromInt(at_phdr)))[0..at_phnum];741 const phdrs = (@as([*]elf.ElfN.Phdr, @ptrFromInt(at_phdr)))[0..at_phnum];
742 expandStackSize(phdrs);742 expandStackSize(phdrs);
743 },743 },
lib/std/zig/system/loongarch.zig+2-2
...@@ -52,9 +52,9 @@ pub fn detectNativeCpuAndFeatures(...@@ -52,9 +52,9 @@ pub fn detectNativeCpuAndFeatures(
52 if (builtin.os.tag == .linux) {52 if (builtin.os.tag == .linux) {
53 const HWCAP = std.os.linux.HWCAP;53 const HWCAP = std.os.linux.HWCAP;
54 const hwcap_bits: usize = if (builtin.link_libc)54 const hwcap_bits: usize = if (builtin.link_libc)
55 std.c.getauxval(std.elf.AT_HWCAP)55 std.c.getauxval(std.elf.AT.HWCAP)
56 else56 else
57 std.os.linux.getauxval(std.elf.AT_HWCAP);57 std.os.linux.getauxval(std.elf.AT.HWCAP);
5858
59 setFeature(&cpu, .ual, (hwcap_bits & HWCAP.UAL) != 0);59 setFeature(&cpu, .ual, (hwcap_bits & HWCAP.UAL) != 0);
6060
test/standalone/glibc_compat/glibc_runtime_check.zig+2-2
...@@ -65,8 +65,8 @@ fn checkGetAuxVal() !void {...@@ -65,8 +65,8 @@ fn checkGetAuxVal() !void {
65}65}
6666
67fn checkGetAuxVal_v2_16() !void {67fn checkGetAuxVal_v2_16() !void {
68 const base = std.c.getauxval(std.elf.AT_BASE);68 const base = std.c.getauxval(std.elf.AT.BASE);
69 const pgsz = std.c.getauxval(std.elf.AT_PAGESZ);69 const pgsz = std.c.getauxval(std.elf.AT.PAGESZ);
7070
71 assert(base != 0);71 assert(base != 0);
72 assert(pgsz != 0);72 assert(pgsz != 0);