Skip to content

Commit fc53fff

Browse files
Fix handling of +whole-archive native link modifier.
1 parent 896f058 commit fc53fff

File tree

9 files changed

+106
-3
lines changed

9 files changed

+106
-3
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+20-3
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};
@@ -2328,6 +2329,7 @@ fn add_upstream_native_libraries(
23282329
.find(|(ty, _)| *ty == crate_type)
23292330
.expect("failed to find crate type in dependency format list");
23302331

2332+
let search_path = OnceCell::new();
23312333
let crates = &codegen_results.crate_info.used_crates;
23322334
let mut last = (NativeLibKind::Unspecified, None);
23332335
for &cnum in crates {
@@ -2352,19 +2354,34 @@ fn add_upstream_native_libraries(
23522354
NativeLibKind::Framework { as_needed } => {
23532355
cmd.link_framework(name, as_needed.unwrap_or(true))
23542356
}
2355-
NativeLibKind::Static { bundle: Some(false), .. } => {
2357+
NativeLibKind::Static { bundle: Some(false), whole_archive } => {
23562358
// Link "static-nobundle" native libs only if the crate they originate from
23572359
// is being linked statically to the current crate. If it's linked dynamically
23582360
// or is an rlib already included via some other dylib crate, the symbols from
23592361
// native libs will have already been included in that dylib.
23602362
if data[cnum.as_usize() - 1] == Linkage::Static {
2361-
cmd.link_staticlib(name, verbatim)
2363+
if whole_archive == Some(true) {
2364+
cmd.link_whole_staticlib(
2365+
name,
2366+
verbatim,
2367+
search_path.get_or_init(|| archive_search_paths(sess)),
2368+
);
2369+
} else {
2370+
cmd.link_staticlib(name, verbatim)
2371+
}
23622372
}
23632373
}
23642374
// ignore statically included native libraries here as we've
23652375
// already included them when we included the rust library
23662376
// previously
2367-
NativeLibKind::Static { bundle: None | Some(true), .. } => {}
2377+
NativeLibKind::Static { bundle: None | Some(true), whole_archive } => {
2378+
if whole_archive == Some(true) {
2379+
bug!(
2380+
"Combining `+bundle` with `+whole-archive` is not allowed. \
2381+
This should have been caught by an earlier validation step already."
2382+
)
2383+
}
2384+
}
23682385
NativeLibKind::RawDylib => {}
23692386
}
23702387
}

compiler/rustc_codegen_ssa/src/lib.rs

+1
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).
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+
}

0 commit comments

Comments
 (0)