Skip to content

Commit 6ed7428

Browse files
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 023856c + 07241e6 commit 6ed7428

13 files changed

+166
-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};
@@ -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,
@@ -2001,7 +2016,7 @@ fn add_local_native_libraries(
20012016
let relevant_libs =
20022017
codegen_results.crate_info.used_libraries.iter().filter(|l| relevant_lib(sess, l));
20032018

2004-
let search_path = archive_search_paths(sess);
2019+
let search_path = OnceCell::new();
20052020
let mut last = (NativeLibKind::Unspecified, None);
20062021
for lib in relevant_libs {
20072022
let name = match lib.name {
@@ -2023,7 +2038,11 @@ fn add_local_native_libraries(
20232038
}
20242039
NativeLibKind::Static { bundle: None | Some(true), .. }
20252040
| NativeLibKind::Static { whole_archive: Some(true), .. } => {
2026-
cmd.link_whole_staticlib(name, verbatim, &search_path);
2041+
cmd.link_whole_staticlib(
2042+
name,
2043+
verbatim,
2044+
&search_path.get_or_init(|| archive_search_paths(sess)),
2045+
);
20272046
}
20282047
NativeLibKind::Static { .. } => cmd.link_staticlib(name, verbatim),
20292048
NativeLibKind::RawDylib => {
@@ -2116,6 +2135,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
21162135
}
21172136

21182137
let mut compiler_builtins = None;
2138+
let search_path = OnceCell::new();
21192139

21202140
for &cnum in deps.iter() {
21212141
if group_start == Some(cnum) {
@@ -2149,16 +2169,35 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
21492169
// external build system already has the native dependencies defined, and it
21502170
// will provide them to the linker itself.
21512171
if sess.opts.debugging_opts.link_native_libraries {
2152-
// Skip if this library is the same as the last.
21532172
let mut last = None;
21542173
for lib in &codegen_results.crate_info.native_libraries[&cnum] {
2155-
if lib.name.is_some()
2156-
&& relevant_lib(sess, lib)
2157-
&& matches!(lib.kind, NativeLibKind::Static { bundle: Some(false), .. })
2158-
&& last != lib.name
2159-
{
2160-
cmd.link_staticlib(lib.name.unwrap(), lib.verbatim.unwrap_or(false));
2161-
last = lib.name;
2174+
if !relevant_lib(sess, lib) {
2175+
// Skip libraries if they are disabled by `#[link(cfg=...)]`
2176+
continue;
2177+
}
2178+
2179+
// Skip if this library is the same as the last.
2180+
if last == lib.name {
2181+
continue;
2182+
}
2183+
2184+
if let Some(static_lib_name) = lib.name {
2185+
if let NativeLibKind::Static { bundle: Some(false), whole_archive } =
2186+
lib.kind
2187+
{
2188+
let verbatim = lib.verbatim.unwrap_or(false);
2189+
if whole_archive == Some(true) {
2190+
cmd.link_whole_staticlib(
2191+
static_lib_name,
2192+
verbatim,
2193+
search_path.get_or_init(|| archive_search_paths(sess)),
2194+
);
2195+
} else {
2196+
cmd.link_staticlib(static_lib_name, verbatim);
2197+
}
2198+
2199+
last = lib.name;
2200+
}
21622201
}
21632202
}
21642203
}

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,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,$@,$<)
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)