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

Allow linking against dylibs in LTO mode #31854

Open
comex opened this issue Feb 24, 2016 · 4 comments
Open

Allow linking against dylibs in LTO mode #31854

comex opened this issue Feb 24, 2016 · 4 comments
Labels
-Cprefer-dynamic Codegen option: Prefer dynamic linking to static linking. A-linkage Area: linking into static, shared libraries and binaries C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@comex
Copy link
Contributor

comex commented Feb 24, 2016

I would like to build a binary that links against some crates using LTO and others dynamically. (Why? Because I have a dynamic library that contains all of LLVM and is 30MB, slowing down linking. Note that this is not LLVM's own dynamic library, it's a Rust crate compiled to dylib that itself links LLVM statically. One might imagine more generic use cases, e.g. LTOing small dependencies and dynamically linking large ones.) Here's a model:

xa.rs:

#![crate_type = "bin"]
extern crate xb;
extern crate xc;
pub fn main() { xb::b(); xc::c(); }

xb.rs:

#![crate_type = "rlib"]
pub fn b() {}

xc.rs:

#![crate_type = "dylib"]
pub fn c() {}

This can be built successfully without LTO, albeit only if the dylib links libstd dynamically (otherwise you get "cannot satisfy dependencies so std only shows up once"):

rustc ../xc.rs -C prefer-dynamic -L. && rustc ../xb.rs -L. && rustc ../xa.rs -L.

The binary xa contains the code from xa.rs and xb.rs linked statically (but with a conventional linker rather than LTO), and dynamically links against libxc.dylib and libstd.

But if the last command includes -C lto, I just get:

error: could not find rlib for: `xc`

It would be nice if rustc supported this.

In principle support could also be added for combining LTO and non-LTO static linking, but that would require some method to identify which is desired, since both types refer to .rlib files.

@alexcrichton
Copy link
Member

This seems like the most reasonable interpretation of "LTO for a dylib" to me, but there'd be a few caveats.

Right now when we create a dynamic Rust library, we keep track of what rlibs are included statically inside of it. That way if anything else needs to rlibs, we know that it get get all the symbols through the dylib. LTO, however, typically internalizes symbols that would otherwise be public, so this would no longer be true. Essentially there'd be no way to re-link against the dylib produced as we wouldn't be sure what symbols survived.

This may, however, be a good addition to my rdylib RFC? That would add a crate type for "a dylib which we can no longer link to", and this seems like the most reasonable interpretation of LTO if there are dylib dependencies as well.

@steveklabnik steveklabnik added the A-linkage Area: linking into static, shared libraries and binaries label Mar 11, 2016
@comex
Copy link
Contributor Author

comex commented Apr 6, 2016

@alexcrichton I just re-read your comment and I'm a little confused - what you're talking about (producing dylibs) seems basically orthogonal to my use case (linking against them). With LTO enabled, you could link a cdylib against only rlibs, an executable against a combination of rlibs and rdylibs, or a cdylib against a combination of rlibs and rdylibs. So I don't see why this would be added to the rdylib RFC.

@alexcrichton
Copy link
Member

Yeah perhaps not, it basically stays the same that you can LTO cdylibs (regardless of dependencies) and you can't LTO rdylibs.

@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 24, 2017
@bjorn3
Copy link
Member

bjorn3 commented May 26, 2023

Since #101403 this is now possible with -Zdylib-lto.

@workingjubilee workingjubilee added the -Cprefer-dynamic Codegen option: Prefer dynamic linking to static linking. label Apr 6, 2024
bherrera pushed a commit to misttech/integration that referenced this issue Feb 11, 2025
This currently only applies to libstd, and automatically to future
dylibs introduced to the build.

We start to properly set `-Zdylib-lto` since https://fxrev.dev/1001661.

Context on linking dylibs in LTO:
rust-lang/rust#31854 (comment)

Original-Bug: 394439548

Original-Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1192917
Original-Revision: 4b5e3f08a544eade31464de8eb4adcbef621eabb
GitOrigin-RevId: 90dbb79a815364391c81e76a778ae599be48a5e4
Change-Id: I652f34a3b365cdad8ee2cd6665c2c250309fc158
bherrera pushed a commit to misttech/mist-os that referenced this issue Feb 11, 2025
This currently only applies to libstd, and automatically to future
dylibs introduced to the build.

We start to properly set `-Zdylib-lto` since https://fxrev.dev/1001661.

Context on linking dylibs in LTO:
rust-lang/rust#31854 (comment)

Bug: 394439548

Change-Id: I48237a739cb1b2bd02c284306d6e82d43eb83cc0
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1192917
Commit-Queue: Jay Zhuang <[email protected]>
Reviewed-by: David Turner <[email protected]>
Reviewed-by: Gulfem Savrun Yeniceri <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
-Cprefer-dynamic Codegen option: Prefer dynamic linking to static linking. A-linkage Area: linking into static, shared libraries and binaries C-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

No branches or pull requests

6 participants