From 731c6d3fbbd935233513f4779e19b4ca18c2006e Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sat, 26 Aug 2023 17:55:12 -0400 Subject: [PATCH 1/4] linux: export elf_aux_maybe so that libraries can call getauxval --- lib/std/os/linux.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 443bf8edf9..a959e195ea 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -153,6 +153,10 @@ pub usingnamespace @import("linux/io_uring.zig"); /// Set by startup code, used by `getauxval`. pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; +pub extern var _elf_aux_maybe: ?[*]std.elf.Auxv; +comptime { + @export(elf_aux_maybe, .{ .name = "_elf_aux_maybe", .linkage = .Weak }); +} /// See `std.elf` for the constants. pub fn getauxval(index: usize) usize { From fba81cd6062df8a49f1ec7559bdaf8d9081fcd43 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sun, 27 Aug 2023 11:12:31 -0400 Subject: [PATCH 2/4] linux: instead of export elf_aux_maybe, export getauxval itself --- lib/std/os/linux.zig | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index a959e195ea..6c96347ac5 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -153,13 +153,11 @@ pub usingnamespace @import("linux/io_uring.zig"); /// Set by startup code, used by `getauxval`. pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; -pub extern var _elf_aux_maybe: ?[*]std.elf.Auxv; -comptime { - @export(elf_aux_maybe, .{ .name = "_elf_aux_maybe", .linkage = .Weak }); -} /// See `std.elf` for the constants. -pub fn getauxval(index: usize) usize { +/// This matches the libc getauxval function. +pub extern fn getauxval(index: usize) usize; +fn getauxvalImpl(index: usize) callconv(.C) usize { const auxv = elf_aux_maybe orelse return 0; var i: usize = 0; while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { @@ -168,6 +166,9 @@ pub fn getauxval(index: usize) usize { } return 0; } +comptime { + @export(getauxvalImpl, .{ .name = "getauxval", .linkage = .Weak }); +} // Some architectures (and some syscalls) require 64bit parameters to be passed // in a even-aligned register pair. From fb0cef8522968af6a15fe953b8f259f3f0a7b37c Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sun, 27 Aug 2023 16:52:43 -0400 Subject: [PATCH 3/4] linux: only export getauxval if not linking libc --- lib/std/os/linux.zig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 6c96347ac5..c967c1dd7b 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -167,7 +167,9 @@ fn getauxvalImpl(index: usize) callconv(.C) usize { return 0; } comptime { - @export(getauxvalImpl, .{ .name = "getauxval", .linkage = .Weak }); + if (!builtin.link_libc) { + @export(getauxvalImpl, .{ .name = "getauxval", .linkage = .Weak }); + } } // Some architectures (and some syscalls) require 64bit parameters to be passed From 2e2d6d2c3dcaccb101c97cf81e7a5f2afdf1ed49 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sun, 27 Aug 2023 19:50:57 -0400 Subject: [PATCH 4/4] linux: fixup for platforms that don't support extern functions yet --- lib/std/os/linux.zig | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index c967c1dd7b..9d3dcac559 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -154,9 +154,21 @@ pub usingnamespace @import("linux/io_uring.zig"); /// Set by startup code, used by `getauxval`. pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; -/// See `std.elf` for the constants. -/// This matches the libc getauxval function. -pub extern fn getauxval(index: usize) usize; +pub usingnamespace if (switch (builtin.zig_backend) { + // Calling extern functions is not yet supported with these backends + .stage2_x86_64, .stage2_aarch64, .stage2_arm, .stage2_riscv64, .stage2_sparc64 => false, + else => !builtin.link_libc, +}) struct { + /// See `std.elf` for the constants. + /// This matches the libc getauxval function. + pub extern fn getauxval(index: usize) usize; + comptime { + @export(getauxvalImpl, .{ .name = "getauxval", .linkage = .Weak }); + } +} else struct { + pub const getauxval = getauxvalImpl; +}; + fn getauxvalImpl(index: usize) callconv(.C) usize { const auxv = elf_aux_maybe orelse return 0; var i: usize = 0; @@ -166,11 +178,6 @@ fn getauxvalImpl(index: usize) callconv(.C) usize { } return 0; } -comptime { - if (!builtin.link_libc) { - @export(getauxvalImpl, .{ .name = "getauxval", .linkage = .Weak }); - } -} // Some architectures (and some syscalls) require 64bit parameters to be passed // in a even-aligned register pair.