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: 9 additions & 9 deletions lib/std/os/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -475,22 +475,22 @@ pub const O = switch (native_arch) {
/// Set by startup code, used by `getauxval`.
pub var elf_aux_maybe: ?[*]std.elf.Auxv = null;

/// Whether an external or internal getauxval implementation is used.
const extern_getauxval = switch (builtin.zig_backend) {
// Calling extern functions is not yet supported with these backends
.stage2_aarch64, .stage2_arm, .stage2_riscv64, .stage2_sparc64 => false,
else => !builtin.link_libc,
};

comptime {
const root = @import("root");
// Export this only when building executable, otherwise it is overriding
// the libc implementation
if (extern_getauxval and (builtin.output_mode == .Exe or @hasDecl(root, "main"))) {
@export(&getauxvalImpl, .{ .name = "getauxval", .linkage = .weak });
}
}

pub const getauxval = if (extern_getauxval) struct {
comptime {
const root = @import("root");
// Export this only when building an executable, otherwise it is overriding
// the libc implementation
if (builtin.output_mode == .Exe or @hasDecl(root, "main")) {
@export(&getauxvalImpl, .{ .name = "getauxval", .linkage = .weak });
}
Comment on lines +488 to +492
Copy link
Member

Choose a reason for hiding this comment

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

Just for my edification, what's the technical reason for needing this @export?

Copy link
Author

@ghost ghost Jan 29, 2025

Choose a reason for hiding this comment

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

Honestly, I don't know. Maybe it can be removed in a different PR.
I'm not really easily able to learn much more about it right now because there hasn't been a new master build for download in over a week.

Copy link
Member

@alexrp alexrp Mar 26, 2025

Choose a reason for hiding this comment

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

I went digging through the history. The reason is that we only want a single getauxval implementation to be used at runtime (only the one that has elf_aux_maybe set from the start code is actually functional), so we're using weak linkage to accomplish that.

This is mainly relevant when linking multiple Zig compilation units together.

}
extern fn getauxval(index: usize) usize;
}.getauxval else getauxvalImpl;

Expand Down