Skip to content

Commit 73641cd

Browse files
committed
Auto merge of #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 #88085. r? `@petrochenkov` cc `@wesleywiser` `@gcoakes`
2 parents 385f8e2 + 70c7317 commit 73641cd

13 files changed

+168
-10
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+49-10
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};
@@ -257,6 +258,19 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
257258
// metadata of the rlib we're generating somehow.
258259
for lib in codegen_results.crate_info.used_libraries.iter() {
259260
match lib.kind {
261+
NativeLibKind::Static { bundle: None | Some(true), whole_archive: Some(true) }
262+
if flavor == RlibFlavor::Normal =>
263+
{
264+
// Don't allow mixing +bundle with +whole_archive since an rlib may contain
265+
// multiple native libs, some of which are +whole-archive and some of which are
266+
// -whole-archive and it isn't clear how we can currently handle such a
267+
// situation correctly.
268+
// See https://github.com/rust-lang/rust/issues/88085#issuecomment-901050897
269+
sess.err(
270+
"the linking modifiers `+bundle` and `+whole-archive` are not compatible \
271+
with each other when generating rlibs",
272+
);
273+
}
260274
NativeLibKind::Static { bundle: None | Some(true), .. } => {}
261275
NativeLibKind::Static { bundle: Some(false), .. }
262276
| NativeLibKind::Dylib { .. }
@@ -1255,6 +1269,7 @@ fn archive_search_paths(sess: &Session) -> Vec<PathBuf> {
12551269
sess.target_filesearch(PathKind::Native).search_path_dirs()
12561270
}
12571271

1272+
#[derive(PartialEq)]
12581273
enum RlibFlavor {
12591274
Normal,
12601275
StaticlibBase,
@@ -2034,7 +2049,7 @@ fn add_local_native_libraries(
20342049
let relevant_libs =
20352050
codegen_results.crate_info.used_libraries.iter().filter(|l| relevant_lib(sess, l));
20362051

2037-
let search_path = archive_search_paths(sess);
2052+
let search_path = OnceCell::new();
20382053
let mut last = (NativeLibKind::Unspecified, None);
20392054
for lib in relevant_libs {
20402055
let name = match lib.name {
@@ -2056,7 +2071,11 @@ fn add_local_native_libraries(
20562071
}
20572072
NativeLibKind::Static { bundle: None | Some(true), .. }
20582073
| NativeLibKind::Static { whole_archive: Some(true), .. } => {
2059-
cmd.link_whole_staticlib(name, verbatim, &search_path);
2074+
cmd.link_whole_staticlib(
2075+
name,
2076+
verbatim,
2077+
&search_path.get_or_init(|| archive_search_paths(sess)),
2078+
);
20602079
}
20612080
NativeLibKind::Static { .. } => cmd.link_staticlib(name, verbatim),
20622081
NativeLibKind::RawDylib => {
@@ -2149,6 +2168,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
21492168
}
21502169

21512170
let mut compiler_builtins = None;
2171+
let search_path = OnceCell::new();
21522172

21532173
for &cnum in deps.iter() {
21542174
if group_start == Some(cnum) {
@@ -2182,16 +2202,35 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
21822202
// external build system already has the native dependencies defined, and it
21832203
// will provide them to the linker itself.
21842204
if sess.opts.debugging_opts.link_native_libraries {
2185-
// Skip if this library is the same as the last.
21862205
let mut last = None;
21872206
for lib in &codegen_results.crate_info.native_libraries[&cnum] {
2188-
if lib.name.is_some()
2189-
&& relevant_lib(sess, lib)
2190-
&& matches!(lib.kind, NativeLibKind::Static { bundle: Some(false), .. })
2191-
&& last != lib.name
2192-
{
2193-
cmd.link_staticlib(lib.name.unwrap(), lib.verbatim.unwrap_or(false));
2194-
last = lib.name;
2207+
if !relevant_lib(sess, lib) {
2208+
// Skip libraries if they are disabled by `#[link(cfg=...)]`
2209+
continue;
2210+
}
2211+
2212+
// Skip if this library is the same as the last.
2213+
if last == lib.name {
2214+
continue;
2215+
}
2216+
2217+
if let Some(static_lib_name) = lib.name {
2218+
if let NativeLibKind::Static { bundle: Some(false), whole_archive } =
2219+
lib.kind
2220+
{
2221+
let verbatim = lib.verbatim.unwrap_or(false);
2222+
if whole_archive == Some(true) {
2223+
cmd.link_whole_staticlib(
2224+
static_lib_name,
2225+
verbatim,
2226+
search_path.get_or_init(|| archive_search_paths(sess)),
2227+
);
2228+
} else {
2229+
cmd.link_staticlib(static_lib_name, verbatim);
2230+
}
2231+
2232+
last = lib.name;
2233+
}
21952234
}
21962235
}
21972236
}

compiler/rustc_codegen_ssa/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![feature(box_patterns)]
44
#![feature(try_blocks)]
55
#![feature(in_band_lifetimes)]
6+
#![feature(once_cell)]
67
#![feature(nll)]
78
#![feature(associated_type_bounds)]
89
#![recursion_limit = "256"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ignore-cross-compile -- compiling C++ code does not work well when cross-compiling
2+
3+
# This test case makes sure that native libraries are linked with --whole-archive semantics
4+
# when the `-bundle,+whole-archive` modifiers are applied to them.
5+
#
6+
# The test works by checking that the resulting executables produce the expected output,
7+
# part of which is emitted by otherwise unreferenced C code. If +whole-archive didn't work
8+
# that code would never make it into the final executable and we'd thus be missing some
9+
# of the output.
10+
11+
-include ../../run-make-fulldeps/tools.mk
12+
13+
all: $(TMPDIR)/$(call BIN,directly_linked) $(TMPDIR)/$(call BIN,indirectly_linked) $(TMPDIR)/$(call BIN,indirectly_linked_via_attr)
14+
$(call RUN,directly_linked) | $(CGREP) 'static-initializer.directly_linked.'
15+
$(call RUN,indirectly_linked) | $(CGREP) 'static-initializer.indirectly_linked.'
16+
$(call RUN,indirectly_linked_via_attr) | $(CGREP) 'static-initializer.native_lib_in_src.'
17+
18+
# Native lib linked directly into executable
19+
$(TMPDIR)/$(call BIN,directly_linked): $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
20+
$(RUSTC) directly_linked.rs -Z unstable-options -l static:+whole-archive=c_static_lib_with_constructor
21+
22+
# Native lib linked into RLIB via `-l static:-bundle,+whole-archive`, RLIB linked into executable
23+
$(TMPDIR)/$(call BIN,indirectly_linked): $(TMPDIR)/librlib_with_cmdline_native_lib.rlib
24+
$(RUSTC) indirectly_linked.rs
25+
26+
# Native lib linked into RLIB via #[link] attribute, RLIB linked into executable
27+
$(TMPDIR)/$(call BIN,indirectly_linked_via_attr): $(TMPDIR)/libnative_lib_in_src.rlib
28+
$(RUSTC) indirectly_linked_via_attr.rs
29+
30+
# Native lib linked into rlib with via commandline
31+
$(TMPDIR)/librlib_with_cmdline_native_lib.rlib: $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
32+
$(RUSTC) rlib_with_cmdline_native_lib.rs -Z unstable-options --crate-type=rlib -l static:-bundle,+whole-archive=c_static_lib_with_constructor
33+
34+
# Native lib linked into rlib via `#[link()]` attribute on extern block.
35+
$(TMPDIR)/libnative_lib_in_src.rlib: $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
36+
$(RUSTC) native_lib_in_src.rs --crate-type=rlib
37+
38+
$(TMPDIR)/libc_static_lib_with_constructor.o: c_static_lib_with_constructor.cpp
39+
$(call COMPILE_OBJ_CXX,$@,$<)
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;
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+
}
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+
}
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+
}
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+
}
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+
}
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() { }
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+
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() { }
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)