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 @@
11//! Executable and Linkable Format.
22
3const builtin = @import("builtin");
34const std = @import("std.zig");
45const Io = std.Io;
56const math = std.math;
67const mem = std.mem;
78const assert = std.debug.assert;
89const Endian = std.builtin.Endian;
9const native_endian = @import("builtin").target.cpu.arch.endian();
10
11pub const AT_NULL = 0;
12pub const AT_IGNORE = 1;
13pub const AT_EXECFD = 2;
14pub const AT_PHDR = 3;
15pub const AT_PHENT = 4;
16pub const AT_PHNUM = 5;
17pub const AT_PAGESZ = 6;
18pub const AT_BASE = 7;
19pub const AT_FLAGS = 8;
20pub const AT_ENTRY = 9;
21pub const AT_NOTELF = 10;
22pub const AT_UID = 11;
23pub const AT_EUID = 12;
24pub const AT_GID = 13;
25pub const AT_EGID = 14;
26pub const AT_CLKTCK = 17;
27pub const AT_PLATFORM = 15;
28pub const AT_HWCAP = 16;
29pub const AT_FPUCW = 18;
30pub const AT_DCACHEBSIZE = 19;
31pub const AT_ICACHEBSIZE = 20;
32pub const AT_UCACHEBSIZE = 21;
33pub const AT_IGNOREPPC = 22;
34pub const AT_SECURE = 23;
35pub const AT_BASE_PLATFORM = 24;
36pub const AT_RANDOM = 25;
37pub const AT_HWCAP2 = 26;
38pub const AT_EXECFN = 31;
39pub const AT_SYSINFO = 32;
40pub const AT_SYSINFO_EHDR = 33;
41pub const AT_L1I_CACHESHAPE = 34;
42pub const AT_L1D_CACHESHAPE = 35;
43pub const AT_L2_CACHESHAPE = 36;
44pub const AT_L3_CACHESHAPE = 37;
45pub const AT_L1I_CACHESIZE = 40;
46pub const AT_L1I_CACHEGEOMETRY = 41;
47pub const AT_L1D_CACHESIZE = 42;
48pub const AT_L1D_CACHEGEOMETRY = 43;
49pub const AT_L2_CACHESIZE = 44;
50pub const AT_L2_CACHEGEOMETRY = 45;
51pub const AT_L3_CACHESIZE = 46;
52pub const AT_L3_CACHEGEOMETRY = 47;
53pub const AT_MINSIGSTKSZ = 51;
10
11const native_endian = builtin.target.cpu.arch.endian();
12const native_os = builtin.target.os.tag;
13
14pub const AT = switch (native_os) {
15 .dragonfly => struct {
16 pub const NULL = 0;
17 pub const IGNORE = 1;
18 pub const EXECFD = 2;
19 pub const PHDR = 3;
20 pub const PHENT = 4;
21 pub const PHNUM = 5;
22 pub const PAGESZ = 6;
23 pub const BASE = 7;
24 pub const FLAGS = 8;
25 pub const ENTRY = 9;
26 pub const NOTELF = 10;
27 pub const UID = 11;
28 pub const EUID = 12;
29 pub const GID = 13;
30 pub const EGID = 14;
31 pub const EXECPATH = 15;
32 pub const CANARY = 16;
33 pub const CANARYLEN = 17;
34 pub const OSRELDATE = 18;
35 pub const NCPUS = 19;
36 pub const PAGESIZES = 20;
37 pub const PAGESIZESLEN = 21;
38 pub const STACKPROT = 23;
39 },
40 .freebsd => struct {
41 pub const NULL = 0;
42 pub const IGNORE = 1;
43 pub const EXECFD = 2;
44 pub const PHDR = 3;
45 pub const PHENT = 4;
46 pub const PHNUM = 5;
47 pub const PAGESZ = 6;
48 pub const BASE = 7;
49 pub const FLAGS = 8;
50 pub const ENTRY = 9;
51 pub const NOTELF = 10;
52 pub const UID = 11;
53 pub const EUID = 12;
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
55233pub const DT_NULL = 0;
56234pub const DT_NEEDED = 1;
lib/std/heap.zig+1-1
......@@ -80,7 +80,7 @@ pub fn defaultQueryPageSize() usize {
8080 .linux => if (builtin.link_libc)
8181 @max(std.c.sysconf(@backingInt(std.c._SC.PAGESIZE)), 0)
8282 else
83 std.os.linux.getauxval(std.elf.AT_PAGESZ),
83 std.os.linux.getauxval(std.elf.AT.PAGESZ),
8484 .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => {
8585 const task_port = std.c.mach_task_self();
8686 // 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 {
705705 @disableInstrumentation();
706706 const auxv = elf_aux_maybe orelse return 0;
707707 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) {
709709 if (auxv[i].a_type == index)
710710 return auxv[i].a_un.a_val;
711711 }
lib/std/os/linux/test.zig+4-4
......@@ -106,10 +106,10 @@ test "statx" {
106106
107107test "user and group ids" {
108108 if (builtin.link_libc) return error.SkipZigTest;
109 try expectEqual(linux.getauxval(elf.AT_UID), linux.getuid());
110 try expectEqual(linux.getauxval(elf.AT_GID), linux.getgid());
111 try expectEqual(linux.getauxval(elf.AT_EUID), linux.geteuid());
112 try expectEqual(linux.getauxval(elf.AT_EGID), linux.getegid());
109 try expectEqual(linux.getauxval(elf.AT.UID), linux.getuid());
110 try expectEqual(linux.getauxval(elf.AT.GID), linux.getgid());
111 try expectEqual(linux.getauxval(elf.AT.EUID), linux.geteuid());
112 try expectEqual(linux.getauxval(elf.AT.EGID), linux.getegid());
113113}
114114
115115test "fadvise" {
lib/std/os/linux/vdso.zig+1-1
......@@ -5,7 +5,7 @@ const mem = std.mem;
55const maxInt = std.math.maxInt;
66
77pub 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);
99 if (vdso_addr == 0) return 0;
1010
1111 const eh = @as(*elf.Ehdr, @ptrFromInt(vdso_addr));
lib/std/posix.zig+4-4
......@@ -775,9 +775,9 @@ pub fn sysctl(
775775
776776pub fn getSelfPhdrs() []std.elf.ElfN.Phdr {
777777 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));
779 const phdrs: [*]std.elf.ElfN.Phdr = @ptrFromInt(getauxval(std.elf.AT_PHDR));
780 return phdrs[0..getauxval(std.elf.AT_PHNUM)];
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));
780 return phdrs[0..getauxval(std.elf.AT.PHNUM)];
781781}
782782
783783pub fn dl_iterate_phdr(
......@@ -819,7 +819,7 @@ pub fn dl_iterate_phdr(
819819 .PHDR => break @intFromPtr(phdrs.ptr) - phdr.vaddr,
820820 else => {},
821821 } else unreachable,
822 .name = switch (getauxval(std.elf.AT_EXECFN)) {
822 .name = switch (getauxval(std.elf.AT.EXECFN)) {
823823 0 => "/proc/self/exe",
824824 else => |name| @ptrFromInt(name),
825825 },
lib/std/start.zig+6-6
......@@ -617,11 +617,11 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {
617617 var i: usize = 0;
618618 var at_phdr: usize = 0;
619619 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) {
621621 switch (auxv[i].a_type) {
622 elf.AT_PHNUM => at_phnum = 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,
622 elf.AT.PHNUM => at_phnum = 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,
625625 else => continue,
626626 }
627627 }
......@@ -736,8 +736,8 @@ fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) cal
736736
737737 switch (builtin.os.tag) {
738738 .linux => {
739 const at_phdr = std.c.getauxval(elf.AT_PHDR);
740 const at_phnum = std.c.getauxval(elf.AT_PHNUM);
739 const at_phdr = std.c.getauxval(elf.AT.PHDR);
740 const at_phnum = std.c.getauxval(elf.AT.PHNUM);
741741 const phdrs = (@as([*]elf.ElfN.Phdr, @ptrFromInt(at_phdr)))[0..at_phnum];
742742 expandStackSize(phdrs);
743743 },
lib/std/zig/system/loongarch.zig+2-2
......@@ -52,9 +52,9 @@ pub fn detectNativeCpuAndFeatures(
5252 if (builtin.os.tag == .linux) {
5353 const HWCAP = std.os.linux.HWCAP;
5454 const hwcap_bits: usize = if (builtin.link_libc)
55 std.c.getauxval(std.elf.AT_HWCAP)
55 std.c.getauxval(std.elf.AT.HWCAP)
5656 else
57 std.os.linux.getauxval(std.elf.AT_HWCAP);
57 std.os.linux.getauxval(std.elf.AT.HWCAP);
5858
5959 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 {
6565}
6666
6767fn checkGetAuxVal_v2_16() !void {
68 const base = std.c.getauxval(std.elf.AT_BASE);
69 const pgsz = std.c.getauxval(std.elf.AT_PAGESZ);
68 const base = std.c.getauxval(std.elf.AT.BASE);
69 const pgsz = std.c.getauxval(std.elf.AT.PAGESZ);
7070
7171 assert(base != 0);
7272 assert(pgsz != 0);