Skip to content

rust: Need to automatically do symbol export handling and --gc-sections when linking a Rust staticlib into another target #11792

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

Open
sdroege opened this issue May 17, 2023 · 3 comments

Comments

@sdroege
Copy link
Contributor

sdroege commented May 17, 2023

Currently when linking a Rust staticlib into an executable or shared library with another linker, the resulting file sizes are multiple times bigger than when having rustc do the job directly.

This happens because lots of unnecessary symbols are exported, and the linker is not removing any unnecessary sections either. Basically, all of the std library and any other Rust crate are included instead of stripping away unnecessary parts.

  1. staticlibs are exporting all public symbols from both the crate itself as well as any Rust dependency that is included inside it, including the standard library. meson's currently approach to symbol visibility is to simply use visibility attributes but that doesn't work in this case because of the "link whole" approach of staticlibs. I'm not sure how this can be solved on the meson side in a useful way. This is kind of related to Add support for symbol visibility export files #4747

  2. staticlibs contain the whole static libraries of their dependencies, so there's a lot of unnecessary code in there. --gc-sections (or -dead_strip on macOS) would get rid of them for executables, and also for shared libraries once 1. is solved. Currently there is no cross-toolchain way of specifying this in meson from what I can see, and this should probably happen automatically when linking in staticlibs anyway.

Somewhat related Rust issues: rust-lang/rust#111593 and rust-lang/rust#111594

@eli-schwartz
Copy link
Member

eli-schwartz commented May 17, 2023

See #8828 and #11213 (the latter case had quite some conversation with @bjorn3, leading I think to the PR mentioned at the end of your second link).

@bonzini
Copy link
Collaborator

bonzini commented Feb 4, 2025

See also rust-lang/rust#73632

@bjorn3
Copy link

bjorn3 commented Feb 4, 2025

rust-lang/rust#73632 is not relevant to linking staticlibs. That issue is about directly linking rlibs without having rustc assemble them into a single staticlib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants