Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux: omit getauxval from binary if not needed #22276

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

wooster0
Copy link
Contributor

@wooster0 wooster0 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.

// This must be done after PIE relocations have been applied or we may crash
// while trying to access the global variable (happens on MIPS at least).
std.os.linux.elf_aux_maybe = auxv;
if (!std.os.linux.extern_getauxval) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I now realize that this check would essentially have to be "if std.os.linux.getauxval was analyzed" or is potentially going to be called somewhere. That makes this a lot harder.

Copy link
Contributor Author

@wooster0 wooster0 Dec 20, 2024

Choose a reason for hiding this comment

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

This should probably be done through an std option. Not doing this for now so just ignore this.

@wooster0 wooster0 force-pushed the bloat branch 2 times, most recently from 7cac9bc to 9304f54 Compare December 20, 2024 10:45
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"))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the extern_getauxval and in this if should be superfluous, because it's already implied by the if (extern_getauxval) around the containing struct.
If that branch isn't taken, the compiler should never analyze this comptime block inside of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point

@wooster0 wooster0 force-pushed the bloat branch 2 times, most recently from 198253f to 2f994d9 Compare December 20, 2024 11:03
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