diff --git a/lib/std/elf.zig b/lib/std/elf.zig index 8ebb002a24efe0801950ca3f4eb284b8a3c0370e..5da1c024488e4dbe115efc45574831417f7e82de 100644 --- a/lib/std/elf.zig +++ b/lib/std/elf.zig @@ -1,56 +1,234 @@ //! Executable and Linkable Format. +const builtin = @import("builtin"); const std = @import("std.zig"); const Io = std.Io; const math = std.math; const mem = std.mem; const assert = std.debug.assert; const Endian = std.builtin.Endian; -const native_endian = @import("builtin").target.cpu.arch.endian(); -pub const AT_NULL = 0; -pub const AT_IGNORE = 1; -pub const AT_EXECFD = 2; -pub const AT_PHDR = 3; -pub const AT_PHENT = 4; -pub const AT_PHNUM = 5; -pub const AT_PAGESZ = 6; -pub const AT_BASE = 7; -pub const AT_FLAGS = 8; -pub const AT_ENTRY = 9; -pub const AT_NOTELF = 10; -pub const AT_UID = 11; -pub const AT_EUID = 12; -pub const AT_GID = 13; -pub const AT_EGID = 14; -pub const AT_CLKTCK = 17; -pub const AT_PLATFORM = 15; -pub const AT_HWCAP = 16; -pub const AT_FPUCW = 18; -pub const AT_DCACHEBSIZE = 19; -pub const AT_ICACHEBSIZE = 20; -pub const AT_UCACHEBSIZE = 21; -pub const AT_IGNOREPPC = 22; -pub const AT_SECURE = 23; -pub const AT_BASE_PLATFORM = 24; -pub const AT_RANDOM = 25; -pub const AT_HWCAP2 = 26; -pub const AT_EXECFN = 31; -pub const AT_SYSINFO = 32; -pub const AT_SYSINFO_EHDR = 33; -pub const AT_L1I_CACHESHAPE = 34; -pub const AT_L1D_CACHESHAPE = 35; -pub const AT_L2_CACHESHAPE = 36; -pub const AT_L3_CACHESHAPE = 37; -pub const AT_L1I_CACHESIZE = 40; -pub const AT_L1I_CACHEGEOMETRY = 41; -pub const AT_L1D_CACHESIZE = 42; -pub const AT_L1D_CACHEGEOMETRY = 43; -pub const AT_L2_CACHESIZE = 44; -pub const AT_L2_CACHEGEOMETRY = 45; -pub const AT_L3_CACHESIZE = 46; -pub const AT_L3_CACHEGEOMETRY = 47; -pub const AT_MINSIGSTKSZ = 51; +const native_endian = builtin.target.cpu.arch.endian(); +const native_os = builtin.target.os.tag; + +pub const AT = switch (native_os) { + .dragonfly => struct { + pub const NULL = 0; + pub const IGNORE = 1; + pub const EXECFD = 2; + pub const PHDR = 3; + pub const PHENT = 4; + pub const PHNUM = 5; + pub const PAGESZ = 6; + pub const BASE = 7; + pub const FLAGS = 8; + pub const ENTRY = 9; + pub const NOTELF = 10; + pub const UID = 11; + pub const EUID = 12; + pub const GID = 13; + pub const EGID = 14; + pub const EXECPATH = 15; + pub const CANARY = 16; + pub const CANARYLEN = 17; + pub const OSRELDATE = 18; + pub const NCPUS = 19; + pub const PAGESIZES = 20; + pub const PAGESIZESLEN = 21; + pub const STACKPROT = 23; + }, + .freebsd => struct { + pub const NULL = 0; + pub const IGNORE = 1; + pub const EXECFD = 2; + pub const PHDR = 3; + pub const PHENT = 4; + pub const PHNUM = 5; + pub const PAGESZ = 6; + pub const BASE = 7; + pub const FLAGS = 8; + pub const ENTRY = 9; + pub const NOTELF = 10; + pub const UID = 11; + pub const EUID = 12; + pub const GID = 13; + pub const EGID = 14; + pub const EXECPATH = 15; + pub const CANARY = 16; + pub const CANARYLEN = 17; + pub const OSRELDATE = 18; + pub const NCPUS = 19; + pub const PAGESIZES = 20; + pub const PAGESIZESLEN = 21; + pub const TIMEKEEP = 22; + pub const STACKPROT = 23; + pub const EHDRFLAGS = 24; + pub const HWCAP = 25; + pub const HWCAP2 = 26; + pub const BSDFLAGS = 27; + pub const ARGC = 28; + pub const ARGV = 29; + pub const ENVC = 30; + pub const ENVV = 31; + pub const PS_STRINGS = 32; + pub const FXRNG = 33; + pub const KPRELOAD = 34; + pub const USRSTACKBASE = 35; + pub const USRSTACKLIM = 36; + pub const CHERI_STATS = 37; + pub const HWCAP3 = 38; + pub const HWCAP4 = 39; + }, + .illumos => struct { + pub const NULL = 0; + pub const IGNORE = 1; + pub const EXECFD = 2; + pub const PHDR = 3; + pub const PHENT = 4; + pub const PHNUM = 5; + pub const PAGESZ = 6; + pub const BASE = 7; + pub const FLAGS = 8; + pub const ENTRY = 9; + pub const NOTELF = 10; + pub const UID = 11; + pub const EUID = 12; + pub const GID = 13; + pub const EGID = 14; + pub const EXECPATH = 15; + pub const CANARY = 16; + pub const CANARYLEN = 17; + pub const OSRELDATE = 18; + pub const NCPUS = 19; + pub const PAGESIZES = 20; + pub const PAGESIZESLEN = 21; + pub const TIMEKEEP = 22; + pub const STACKPROT = 23; + }, + .linux => struct { + pub const NULL = 0; + pub const IGNORE = 1; + pub const EXECFD = 2; + pub const PHDR = 3; + pub const PHENT = 4; + pub const PHNUM = 5; + pub const PAGESZ = 6; + pub const BASE = 7; + pub const FLAGS = 8; + pub const ENTRY = 9; + pub const NOTELF = 10; + pub const UID = 11; + pub const EUID = 12; + pub const GID = 13; + pub const EGID = 14; + pub const CLKTCK = 17; + pub const PLATFORM = 15; + pub const HWCAP = 16; + pub const FPUCW = 18; + pub const DCACHEBSIZE = 19; + pub const ICACHEBSIZE = 20; + pub const UCACHEBSIZE = 21; + pub const IGNOREPPC = 22; + pub const SECURE = 23; + pub const BASE_PLATFORM = 24; + pub const RANDOM = 25; + pub const HWCAP2 = 26; + pub const EXECFN = 31; + pub const SYSINFO = 32; + pub const SYSINFO_EHDR = 33; + pub const L1I_CACHESHAPE = 34; + pub const L1D_CACHESHAPE = 35; + pub const L2_CACHESHAPE = 36; + pub const L3_CACHESHAPE = 37; + pub const L1I_CACHESIZE = 40; + pub const L1I_CACHEGEOMETRY = 41; + pub const L1D_CACHESIZE = 42; + pub const L1D_CACHEGEOMETRY = 43; + pub const L2_CACHESIZE = 44; + pub const L2_CACHEGEOMETRY = 45; + pub const L3_CACHESIZE = 46; + pub const L3_CACHEGEOMETRY = 47; + pub const MINSIGSTKSZ = 51; + }, + .netbsd => struct { + pub const NULL = 0; + pub const IGNORE = 1; + pub const EXECFD = 2; + pub const PHDR = 3; + pub const PHENT = 4; + pub const PHNUM = 5; + pub const PAGESZ = 6; + pub const BASE = 7; + pub const FLAGS = 8; + pub const ENTRY = 9; + pub const MIPS_NOTELF = 10; + pub const DCACHEBSIZE = 10; + pub const ICACHEBSIZE = 11; + pub const UCACHEBSIZE = 12; + pub const STACKBASE = 13; + pub const EUID = 2000; + pub const RUID = 2001; + pub const EGID = 2002; + pub const RGID = 2003; + pub const SUN_LDELF = 2004; + pub const SUN_LDSHDR = 2005; + pub const SUN_LDNAME = 2006; + pub const SUN_LPGSIZE = 2007; + pub const SUN_PLATFORM = 2008; + pub const SUN_HWCAP = 2009; + pub const SUN_IFLUSH = 2010; + pub const SUN_CPU = 2011; + pub const SUN_EMUL_ENTRY = 2012; + pub const SUN_EMUL_EXECFD = 2013; + pub const SUN_EXECNAME = 2014; + }, + .openbsd => struct { + pub const NULL = 0; + pub const IGNORE = 1; + pub const PAGESZ = 6; + pub const HWCAP = 25; + pub const HWCAP2 = 26; + }, + .serenity => struct { + pub const NULL = 0; + pub const IGNORE = 1; + pub const EXECFD = 2; + pub const PHDR = 3; + pub const PHENT = 4; + pub const PHNUM = 5; + pub const PAGESZ = 6; + pub const BASE = 7; + pub const FLAGS = 8; + pub const ENTRY = 9; + pub const NOTELF = 10; + pub const UID = 11; + pub const EUID = 12; + pub const GID = 13; + pub const PLATFORM = 15; + pub const HWCAP = 16; + pub const CLKTCK = 17; + pub const SECURE = 23; + pub const BASE_PLATFORM = 24; + pub const RANDOM = 25; + pub const HWCAP2 = 26; + pub const EXECFN = 31; + pub const EXE_BASE = 32; + pub const EXE_SIZE = 33; + }, + else => struct { + pub const NULL = 0; + pub const IGNORE = 1; + pub const EXECFD = 2; + pub const PHDR = 3; + pub const PHENT = 4; + pub const PHNUM = 5; + pub const PAGESZ = 6; + pub const BASE = 7; + pub const FLAGS = 8; + pub const ENTRY = 9; + pub const NOTELF = 10; + }, +}; pub const DT_NULL = 0; pub const DT_NEEDED = 1; diff --git a/lib/std/heap.zig b/lib/std/heap.zig index 45c509e09bb74bcb834543461b9ad99dc54ce5fd..12a7f0a52eb131492e9dc5be72b42acec263373b 100644 --- a/lib/std/heap.zig +++ b/lib/std/heap.zig @@ -80,7 +80,7 @@ pub fn defaultQueryPageSize() usize { .linux => if (builtin.link_libc) @max(std.c.sysconf(@backingInt(std.c._SC.PAGESIZE)), 0) else - std.os.linux.getauxval(std.elf.AT_PAGESZ), + std.os.linux.getauxval(std.elf.AT.PAGESZ), .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => { const task_port = std.c.mach_task_self(); // mach_task_self may fail "if there are any resource failures or other errors". diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 80d25ef865f6ac69fc58b711dccbed5d6cebe27a..0f90d004262bc5baf645fe120af4246022d89e49 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -705,7 +705,7 @@ fn getauxvalImpl(index: usize) callconv(.c) usize { @disableInstrumentation(); const auxv = elf_aux_maybe orelse return 0; var i: usize = 0; - while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { + while (auxv[i].a_type != std.elf.AT.NULL) : (i += 1) { if (auxv[i].a_type == index) return auxv[i].a_un.a_val; } diff --git a/lib/std/os/linux/test.zig b/lib/std/os/linux/test.zig index 56c7c96c9432055945b71bca80de990d58df497d..1a22e8f24a264d960040adde43a0070dc9d2fc96 100644 --- a/lib/std/os/linux/test.zig +++ b/lib/std/os/linux/test.zig @@ -106,10 +106,10 @@ test "statx" { test "user and group ids" { if (builtin.link_libc) return error.SkipZigTest; - try expectEqual(linux.getauxval(elf.AT_UID), linux.getuid()); - try expectEqual(linux.getauxval(elf.AT_GID), linux.getgid()); - try expectEqual(linux.getauxval(elf.AT_EUID), linux.geteuid()); - try expectEqual(linux.getauxval(elf.AT_EGID), linux.getegid()); + try expectEqual(linux.getauxval(elf.AT.UID), linux.getuid()); + try expectEqual(linux.getauxval(elf.AT.GID), linux.getgid()); + try expectEqual(linux.getauxval(elf.AT.EUID), linux.geteuid()); + try expectEqual(linux.getauxval(elf.AT.EGID), linux.getegid()); } test "fadvise" { diff --git a/lib/std/os/linux/vdso.zig b/lib/std/os/linux/vdso.zig index 53be1bf9d51f0048d57074313fa49aff65e0e6a2..bdf46337b59ecc863a543d3722ddf47bd967374a 100644 --- a/lib/std/os/linux/vdso.zig +++ b/lib/std/os/linux/vdso.zig @@ -5,7 +5,7 @@ const mem = std.mem; const maxInt = std.math.maxInt; pub fn lookup(vername: []const u8, name: []const u8) usize { - const vdso_addr = linux.getauxval(std.elf.AT_SYSINFO_EHDR); + const vdso_addr = linux.getauxval(std.elf.AT.SYSINFO_EHDR); if (vdso_addr == 0) return 0; const eh = @as(*elf.Ehdr, @ptrFromInt(vdso_addr)); diff --git a/lib/std/posix.zig b/lib/std/posix.zig index 803fa9a9054f974ee2371011c938a96b8b72e02d..712b093e71f458810d4a8e11e451c7a44f9905d1 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -775,9 +775,9 @@ pub fn sysctl( pub fn getSelfPhdrs() []std.elf.ElfN.Phdr { const getauxval = if (builtin.link_libc) std.c.getauxval else std.os.linux.getauxval; - assert(getauxval(std.elf.AT_PHENT) == @sizeOf(std.elf.ElfN.Phdr)); - const phdrs: [*]std.elf.ElfN.Phdr = @ptrFromInt(getauxval(std.elf.AT_PHDR)); - return phdrs[0..getauxval(std.elf.AT_PHNUM)]; + assert(getauxval(std.elf.AT.PHENT) == @sizeOf(std.elf.ElfN.Phdr)); + const phdrs: [*]std.elf.ElfN.Phdr = @ptrFromInt(getauxval(std.elf.AT.PHDR)); + return phdrs[0..getauxval(std.elf.AT.PHNUM)]; } pub fn dl_iterate_phdr( @@ -819,7 +819,7 @@ pub fn dl_iterate_phdr( .PHDR => break @intFromPtr(phdrs.ptr) - phdr.vaddr, else => {}, } else unreachable, - .name = switch (getauxval(std.elf.AT_EXECFN)) { + .name = switch (getauxval(std.elf.AT.EXECFN)) { 0 => "/proc/self/exe", else => |name| @ptrFromInt(name), }, diff --git a/lib/std/start.zig b/lib/std/start.zig index 28b997fd7ee2bd7d49034d189b638a5d8cd5f69b..d86e6172523d7b3999e04c3fee07633792630a9d 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -617,11 +617,11 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn { var i: usize = 0; var at_phdr: usize = 0; var at_phnum: usize = 0; - while (auxv[i].a_type != elf.AT_NULL) : (i += 1) { + while (auxv[i].a_type != elf.AT.NULL) : (i += 1) { switch (auxv[i].a_type) { - elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val, - elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val, - elf.AT_HWCAP => at_hwcap = auxv[i].a_un.a_val, + elf.AT.PHNUM => at_phnum = auxv[i].a_un.a_val, + elf.AT.PHDR => at_phdr = auxv[i].a_un.a_val, + elf.AT.HWCAP => at_hwcap = auxv[i].a_un.a_val, else => continue, } } @@ -736,8 +736,8 @@ fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) cal switch (builtin.os.tag) { .linux => { - const at_phdr = std.c.getauxval(elf.AT_PHDR); - const at_phnum = std.c.getauxval(elf.AT_PHNUM); + const at_phdr = std.c.getauxval(elf.AT.PHDR); + const at_phnum = std.c.getauxval(elf.AT.PHNUM); const phdrs = (@as([*]elf.ElfN.Phdr, @ptrFromInt(at_phdr)))[0..at_phnum]; expandStackSize(phdrs); }, diff --git a/lib/std/zig/system/loongarch.zig b/lib/std/zig/system/loongarch.zig index eee112462d9de7e17466c34578069c3a6058d4f3..1476eab209d5851974b4fa3b866ad40778cf0560 100644 --- a/lib/std/zig/system/loongarch.zig +++ b/lib/std/zig/system/loongarch.zig @@ -52,9 +52,9 @@ pub fn detectNativeCpuAndFeatures( if (builtin.os.tag == .linux) { const HWCAP = std.os.linux.HWCAP; const hwcap_bits: usize = if (builtin.link_libc) - std.c.getauxval(std.elf.AT_HWCAP) + std.c.getauxval(std.elf.AT.HWCAP) else - std.os.linux.getauxval(std.elf.AT_HWCAP); + std.os.linux.getauxval(std.elf.AT.HWCAP); setFeature(&cpu, .ual, (hwcap_bits & HWCAP.UAL) != 0); diff --git a/test/standalone/glibc_compat/glibc_runtime_check.zig b/test/standalone/glibc_compat/glibc_runtime_check.zig index 7c5e10e9e94edc71ec0e1ec3dabbb1e38b3ab88c..2dc565cbe7c926170a59b395f694180fdfe7612b 100644 --- a/test/standalone/glibc_compat/glibc_runtime_check.zig +++ b/test/standalone/glibc_compat/glibc_runtime_check.zig @@ -65,8 +65,8 @@ fn checkGetAuxVal() !void { } fn checkGetAuxVal_v2_16() !void { - const base = std.c.getauxval(std.elf.AT_BASE); - const pgsz = std.c.getauxval(std.elf.AT_PAGESZ); + const base = std.c.getauxval(std.elf.AT.BASE); + const pgsz = std.c.getauxval(std.elf.AT.PAGESZ); assert(base != 0); assert(pgsz != 0);