Skip to content

Commit 60abbb6

Browse files
authored
Rollup merge of rust-lang#88161 - michaelwoerister:fix-whole-archive-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`
2 parents aaffdde + 2899c45 commit 60abbb6

13 files changed

+151
-3
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use regex::Regex;
3636
use tempfile::Builder as TempFileBuilder;
3737

3838
use std::ffi::OsString;
39+
use std::lazy::OnceCell;
3940
use std::path::{Path, PathBuf};
4041
use std::process::{ExitStatus, Output, Stdio};
4142
use std::{ascii, char, env, fmt, fs, io, mem, str};
@@ -254,6 +255,19 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
254255
// metadata of the rlib we're generating somehow.
255256
for lib in codegen_results.crate_info.used_libraries.iter() {
256257
match lib.kind {
258+
NativeLibKind::Static { bundle: None | Some(true), whole_archive: Some(true) }
259+
if flavor == RlibFlavor::Normal =>
260+
{
261+
// Don't allow mixing +bundle with +whole_archive since an rlib may contain
262+
// multiple native libs, some of which are +whole-archive and some of which are
263+
// -whole-archive and it isn't clear how we can currently handle such a
264+
// situation correctly.
265+
// See https://github.com/rust-lang/rust/issues/88085#issuecomment-901050897
266+
sess.err(
267+
"the linking modifiers `+bundle` and `+whole-archive` are not compatible \
268+
with each other when generating rlibs",
269+
);
270+
}
257271
NativeLibKind::Static { bundle: None | Some(true), .. } => {}
258272
NativeLibKind::Static { bundle: Some(false), .. }
259273
| NativeLibKind::Dylib { .. }
@@ -1222,6 +1236,7 @@ pub fn archive_search_paths(sess: &Session) -> Vec<PathBuf> {
12221236
sess.target_filesearch(PathKind::Native).search_path_dirs()
12231237
}
12241238

1239+
#[derive(PartialEq)]
12251240
enum RlibFlavor {
12261241
Normal,
12271242
StaticlibBase,
@@ -2328,6 +2343,7 @@ fn add_upstream_native_libraries(
23282343
.find(|(ty, _)| *ty == crate_type)
23292344
.expect("failed to find crate type in dependency format list");
23302345

2346+
let search_path = OnceCell::new();
23312347
let crates = &codegen_results.crate_info.used_crates;
23322348
let mut last = (NativeLibKind::Unspecified, None);
23332349
for &cnum in crates {
@@ -2352,19 +2368,34 @@ fn add_upstream_native_libraries(
23522368
NativeLibKind::Framework { as_needed } => {
23532369
cmd.link_framework(name, as_needed.unwrap_or(true))
23542370
}
2355-
NativeLibKind::Static { bundle: Some(false), .. } => {
2371+
NativeLibKind::Static { bundle: Some(false), whole_archive } => {
23562372
// Link "static-nobundle" native libs only if the crate they originate from
23572373
// is being linked statically to the current crate. If it's linked dynamically
23582374
// or is an rlib already included via some other dylib crate, the symbols from
23592375
// native libs will have already been included in that dylib.
23602376
if data[cnum.as_usize() - 1] == Linkage::Static {
2361-
cmd.link_staticlib(name, verbatim)
2377+
if whole_archive == Some(true) {
2378+
cmd.link_whole_staticlib(
2379+
name,
2380+
verbatim,
2381+
search_path.get_or_init(|| archive_search_paths(sess)),
2382+
);
2383+
} else {
2384+
cmd.link_staticlib(name, verbatim)
2385+
}
23622386
}
23632387
}
23642388
// ignore statically included native libraries here as we've
23652389
// already included them when we included the rust library
23662390
// previously
2367-
NativeLibKind::Static { bundle: None | Some(true), .. } => {}
2391+
NativeLibKind::Static { bundle: None | Some(true), whole_archive } => {
2392+
if whole_archive == Some(true) {
2393+
bug!(
2394+
"Combining `+bundle` with `+whole-archive` is not allowed. \
2395+
This should have been caught by an earlier validation step already."
2396+
)
2397+
}
2398+
}
23682399
NativeLibKind::RawDylib => {}
23692400
}
23702401
}

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(in_band_lifetimes)]
66
#![feature(nll)]
77
#![feature(associated_type_bounds)]
8+
#![feature(once_cell)]
89
#![recursion_limit = "256"]
910

1011
//! This crate contains codegen code that is used by all codegen backends (LLVM and others).
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This test case makes sure that native libraries are linked with --whole-archive semantics
2+
# when the `-bundle,+whole-archive` modifiers are applied to them.
3+
#
4+
# The test works by checking that the resulting executables produce the expected output,
5+
# part of which is emitted by otherwise unreferenced C code. If +whole-archive didn't work
6+
# that code would never make it into the final executable and we'd thus be missing some
7+
# of the output.
8+
9+
-include ../../run-make-fulldeps/tools.mk
10+
11+
all: $(TMPDIR)/$(call BIN,directly_linked) $(TMPDIR)/$(call BIN,indirectly_linked) $(TMPDIR)/$(call BIN,indirectly_linked_via_attr)
12+
$(call RUN,directly_linked) | $(CGREP) 'static-initializer.directly_linked.'
13+
$(call RUN,indirectly_linked) | $(CGREP) 'static-initializer.indirectly_linked.'
14+
$(call RUN,indirectly_linked_via_attr) | $(CGREP) 'static-initializer.native_lib_in_src.'
15+
16+
# Native lib linked directly into executable
17+
$(TMPDIR)/$(call BIN,directly_linked): $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
18+
$(RUSTC) directly_linked.rs -Z unstable-options -l static:+whole-archive=c_static_lib_with_constructor
19+
20+
# Native lib linked into RLIB via `-l static:-bundle,+whole-archive`, RLIB linked into executable
21+
$(TMPDIR)/$(call BIN,indirectly_linked): $(TMPDIR)/librlib_with_cmdline_native_lib.rlib
22+
$(RUSTC) indirectly_linked.rs
23+
24+
# Native lib linked into RLIB via #[link] attribute, RLIB linked into executable
25+
$(TMPDIR)/$(call BIN,indirectly_linked_via_attr): $(TMPDIR)/libnative_lib_in_src.rlib
26+
$(RUSTC) indirectly_linked_via_attr.rs
27+
28+
# Native lib linked into rlib with via commandline
29+
$(TMPDIR)/librlib_with_cmdline_native_lib.rlib: $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
30+
$(RUSTC) rlib_with_cmdline_native_lib.rs -Z unstable-options --crate-type=rlib -l static:-bundle,+whole-archive=c_static_lib_with_constructor
31+
32+
# Native lib linked into rlib via `#[link()]` attribute on extern block.
33+
$(TMPDIR)/libnative_lib_in_src.rlib: $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
34+
$(RUSTC) native_lib_in_src.rs --crate-type=rlib
35+
36+
$(TMPDIR)/libc_static_lib_with_constructor.o: c_static_lib_with_constructor.cpp
37+
$(call COMPILE_OBJ_CXX,$@,$<)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <cstdio>
2+
3+
// Since this is a global variable, its constructor will be called before
4+
// main() is executed. But only if the object file containing it actually
5+
// gets linked into the executable.
6+
struct Foo {
7+
Foo() {
8+
printf("static-initializer.");
9+
fflush(stdout);
10+
}
11+
} FOO;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use std::io::Write;
2+
3+
fn main() {
4+
print!("directly_linked.");
5+
std::io::stdout().flush().unwrap();
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate rlib_with_cmdline_native_lib;
2+
3+
fn main() {
4+
rlib_with_cmdline_native_lib::hello();
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate native_lib_in_src;
2+
3+
fn main() {
4+
native_lib_in_src::hello();
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(native_link_modifiers_bundle)]
2+
#![feature(native_link_modifiers_whole_archive)]
3+
#![feature(native_link_modifiers)]
4+
5+
use std::io::Write;
6+
7+
#[link(name = "c_static_lib_with_constructor",
8+
kind = "static",
9+
modifiers = "-bundle,+whole-archive")]
10+
extern {}
11+
12+
pub fn hello() {
13+
print!("native_lib_in_src.");
14+
std::io::stdout().flush().unwrap();
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use std::io::Write;
2+
3+
pub fn hello() {
4+
print!("indirectly_linked.");
5+
std::io::stdout().flush().unwrap();
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// compile-flags: -Zunstable-options --crate-type rlib
2+
// build-fail
3+
// error-pattern: the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs
4+
5+
#![feature(native_link_modifiers)]
6+
#![feature(native_link_modifiers_bundle)]
7+
#![feature(native_link_modifiers_whole_archive)]
8+
9+
#[link(name = "mylib", kind = "static", modifiers = "+bundle,+whole-archive")]
10+
extern "C" { }
11+
12+
fn main() { }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs
2+
3+
error: could not find native static library `mylib`, perhaps an -L flag is missing?
4+
5+
error: aborting due to 2 previous errors
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Mixing +bundle and +whole-archive is not allowed
2+
3+
// compile-flags: -l static:+bundle,+whole-archive=mylib -Zunstable-options --crate-type rlib
4+
// build-fail
5+
// error-pattern: the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs
6+
7+
fn main() { }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs
2+
3+
error: could not find native static library `mylib`, perhaps an -L flag is missing?
4+
5+
error: aborting due to 2 previous errors
6+

0 commit comments

Comments
 (0)