Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/std/os/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,22 @@ 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.
pub 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 });
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, we are exporting it as weak because we expect it to be overriden by a different version in an exe/dso that has getauxval hooked up into startup routine? What if we're building an exe and link with a dso? Which version takes precedence then? Do I even understand the problem correctly here?

Copy link
Copy Markdown
Contributor Author

@kcbanner kcbanner Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, we are exporting it as weak because we expect it to be overriden by a different version in an exe/dso that has getauxval hooked up into startup routine?

Yes, exactly. The intention is that the version used is the one that references the elf_aux_maybe initialized by the startup code.

What if we're building an exe and link with a dso? Which version takes precedence then? Do I even understand the problem correctly here?

My assumption had been that the version in the exe would take precedence, but if this is not true in all cases then this solution won't be adequate.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha thanks!

}
} 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;
while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
Expand Down