-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Whole archive modifier for native libraries is not propagated to final output. #88085
Comments
If we want to support that use case, rlibs will need to stop being "just static libraries" containing object files, they'll need to maintain some kind of internal structure to be able to be broken into parts with those parts being passed to linker separately. Short term we probably need to make |
@petrochenkov, I just tried adding |
I can reproduce the issue as described:
That it does not work for the no-bundle case seems to be a bug in rust/compiler/rustc_codegen_ssa/src/back/link.rs Lines 2355 to 2363 in aa8f27b
Changing this to the following makes the no-bundle case work for me locally: NativeLibKind::Static { bundle: Some(false), whole_archive } => {
// Link "static-nobundle" native libs only if the crate they originate from
// is being linked statically to the current crate. If it's linked dynamically
// or is an rlib already included via some other dylib crate, the symbols from
// native libs will have already been included in that dylib.
if data[cnum.as_usize() - 1] == Linkage::Static {
if whole_archive == Some(true) {
cmd.link_whole_staticlib(name, verbatim, &search_path);
} else {
cmd.link_staticlib(name, verbatim)
}
}
} |
@petrochenkov, I plan to make a PR with the above fix and some regression tests. However, the fix only covers the
Are there any better solutions? |
I don't think this is a right thing to do - a rlib can include its own code and multiple native libraries with different values of the
These looks like good near term solutions.
I'd be interested in seeing a design doc describing how to build a rlib in a way allowing to "disassemble" it into parts and link those parts separately. It would be useful not only for |
How would this look from a backwards-compatibility point of view? It should be fine because |
Yes. |
@michaelwoerister, the |
@gcoakes, yes that case is clearly just a bug in the implementation. I'm working on a fix. |
…no-bundle, r=petrochenkov Fix handling of +whole-archive native link modifier. This PR fixes a bug in `add_upstream_native_libraries` that led to the `+whole-archive` modifier being ignored when linking in native libs. ~~Note that the PR does not address the situation when `+whole-archive` is combined with `+bundle`.~~ `@wesleywiser's` commit adds validation code that turns combining `+whole-archive` with `+bundle` into an error. Fixes rust-lang#88085. r? `@petrochenkov` cc `@wesleywiser` `@gcoakes`
…no-bundle, r=petrochenkov Fix handling of +whole-archive native link modifier. This PR fixes a bug in `add_upstream_native_libraries` that led to the `+whole-archive` modifier being ignored when linking in native libs. ~~Note that the PR does not address the situation when `+whole-archive` is combined with `+bundle`.~~ `@wesleywiser's` commit adds validation code that turns combining `+whole-archive` with `+bundle` into an error. Fixes rust-lang#88085. r? `@petrochenkov` cc `@wesleywiser` `@gcoakes`
With the additional of RFC 2951 (tracking: #81490, implementation: #83507), linking modifiers such as
+whole-archive
are now possible to use for native libraries. However,+whole-archive
is not propagated to the final output of the build. I've minimally reproduced this issue as best I could here. It can probably be minimized more with raw calls to rustc, but I'm not familiar enough to do so right now.Essentially, I have a static library which provides constructor and destructor functions which must be included in the final output.
I tried this code:
foo.c
build.rs
lib.rs
main.rs
I expected to see this happen:
Instead, this happened:
Resolution
I've done a bit of code inspection to determine where the linking issue actually is. I think it comes down to the
link_upstream
closure inrustc_codegen_ssa::src::back::link::add_upstream_rust_crates::add_static_crate
. It should be adjusted to inspect the crates native libraries and determine if any of them have+whole-archive
. If one or more do, then it should runlink_whole_rlib
on the crate's rlib instead oflink_rlib
.Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: