Skip to content

linux: omit getauxval from binary if not needed#22276

Merged
alexrp merged 1 commit into
masterfrom
unknown repository
Mar 26, 2025
Merged

linux: omit getauxval from binary if not needed#22276
alexrp merged 1 commit into
masterfrom
unknown repository

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Dec 20, 2024

Fixes #21522

@rohlem's suggestion in #21522 to move the relevant comptime block into the container namespace used to define getauxval seems to work fine.

$ cat hello_world.zig
const std = @import("std");

pub const std_options = std.Options{ .keep_sigpipe = true };

pub fn main() void {
    std.io.getStdOut().writeAll("hello world\n") catch {};
}
$ zig build-exe hello_world.zig -OReleaseSmall -fsingle-threaded && wc -c hello_world
2016 hello_world
$ zig build-exe hello_world.zig -OReleaseSmall -fsingle-threaded --zig-lib-dir lib && wc -c hello_world
1944 hello_world
$

Before: 2016 bytes. After: 1944 bytes.

Might still not address all cases but I guess a new more specific issue can be opened for that.

Comment thread lib/std/start.zig Outdated
Comment thread lib/std/os/linux.zig Outdated
Comment thread lib/std/os/linux.zig
Comment on lines +488 to +492
// 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 });
}
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.

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

Copy link
Copy Markdown
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
Copy Markdown
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.

@alexrp alexrp merged commit 263ba34 into ziglang:master Mar 26, 2025
@alexrp alexrp added this to the 0.14.1 milestone Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getauxv bloat in static exe

2 participants