Skip to content

Commit 24c8985

Browse files
committed
Auto merge of #96436 - petrochenkov:nowhole2, r=wesleywiser
linker: Stop using whole-archive on dependencies of dylibs #95604 implemented a better and more fine-grained way of keeping exported symbols alive. Addresses the second question from #93901 (comment). r? `@wesleywiser`
2 parents 879fb42 + 4136b8f commit 24c8985

File tree

3 files changed

+10
-46
lines changed

3 files changed

+10
-46
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+6-31
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,7 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
19471947
// This change is somewhat breaking in practice due to local static libraries being linked
19481948
// as whole-archive (#85144), so removing whole-archive may be a pre-requisite.
19491949
if sess.opts.debugging_opts.link_native_libraries {
1950-
add_local_native_libraries(cmd, sess, codegen_results, crate_type);
1950+
add_local_native_libraries(cmd, sess, codegen_results);
19511951
}
19521952

19531953
// Upstream rust libraries and their nobundle static libraries
@@ -2119,16 +2119,6 @@ fn add_order_independent_options(
21192119
add_rpath_args(cmd, sess, codegen_results, out_filename);
21202120
}
21212121

2122-
// A dylib may reexport symbols from the linked rlib or native static library.
2123-
// Even if some symbol is reexported it's still not necessarily counted as used and may be
2124-
// dropped, at least with `ld`-like ELF linkers. So we have to link some rlibs and static
2125-
// libraries as whole-archive to avoid losing reexported symbols.
2126-
// FIXME: Find a way to mark reexported symbols as used and avoid this use of whole-archive.
2127-
fn default_to_whole_archive(sess: &Session, crate_type: CrateType, cmd: &dyn Linker) -> bool {
2128-
crate_type == CrateType::Dylib
2129-
&& !(sess.target.limit_rdylib_exports && cmd.exported_symbol_means_used_symbol())
2130-
}
2131-
21322122
/// # Native library linking
21332123
///
21342124
/// User-supplied library search paths (-L on the command line). These are the same paths used to
@@ -2142,7 +2132,6 @@ fn add_local_native_libraries(
21422132
cmd: &mut dyn Linker,
21432133
sess: &Session,
21442134
codegen_results: &CodegenResults,
2145-
crate_type: CrateType,
21462135
) {
21472136
let filesearch = sess.target_filesearch(PathKind::All);
21482137
for search_path in filesearch.search_paths() {
@@ -2184,7 +2173,6 @@ fn add_local_native_libraries(
21842173
}
21852174
NativeLibKind::Static { whole_archive, bundle, .. } => {
21862175
if whole_archive == Some(true)
2187-
|| (whole_archive == None && default_to_whole_archive(sess, crate_type, cmd))
21882176
// Backward compatibility case: this can be a rlib (so `+whole-archive` cannot
21892177
// be added explicitly if necessary, see the error in `fn link_rlib`) compiled
21902178
// as an executable due to `--test`. Use whole-archive implicitly, like before
@@ -2303,7 +2291,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
23032291
let src = &codegen_results.crate_info.used_crate_source[&cnum];
23042292
match data[cnum.as_usize() - 1] {
23052293
_ if codegen_results.crate_info.profiler_runtime == Some(cnum) => {
2306-
add_static_crate::<B>(cmd, sess, codegen_results, tmpdir, crate_type, cnum);
2294+
add_static_crate::<B>(cmd, sess, codegen_results, tmpdir, cnum);
23072295
}
23082296
// compiler-builtins are always placed last to ensure that they're
23092297
// linked correctly.
@@ -2313,7 +2301,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
23132301
}
23142302
Linkage::NotLinked | Linkage::IncludedFromDylib => {}
23152303
Linkage::Static => {
2316-
add_static_crate::<B>(cmd, sess, codegen_results, tmpdir, crate_type, cnum);
2304+
add_static_crate::<B>(cmd, sess, codegen_results, tmpdir, cnum);
23172305

23182306
// Link static native libs with "-bundle" modifier only if the crate they originate from
23192307
// is being linked statically to the current crate. If it's linked dynamically
@@ -2344,10 +2332,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
23442332
lib.kind
23452333
{
23462334
let verbatim = lib.verbatim.unwrap_or(false);
2347-
if whole_archive == Some(true)
2348-
|| (whole_archive == None
2349-
&& default_to_whole_archive(sess, crate_type, cmd))
2350-
{
2335+
if whole_archive == Some(true) {
23512336
cmd.link_whole_staticlib(
23522337
name,
23532338
verbatim,
@@ -2374,7 +2359,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
23742359
// was already "included" in a dylib (e.g., `libstd` when `-C prefer-dynamic`
23752360
// is used)
23762361
if let Some(cnum) = compiler_builtins {
2377-
add_static_crate::<B>(cmd, sess, codegen_results, tmpdir, crate_type, cnum);
2362+
add_static_crate::<B>(cmd, sess, codegen_results, tmpdir, cnum);
23782363
}
23792364

23802365
// Converts a library file-stem into a cc -l argument
@@ -2405,23 +2390,13 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
24052390
sess: &'a Session,
24062391
codegen_results: &CodegenResults,
24072392
tmpdir: &Path,
2408-
crate_type: CrateType,
24092393
cnum: CrateNum,
24102394
) {
24112395
let src = &codegen_results.crate_info.used_crate_source[&cnum];
24122396
let cratepath = &src.rlib.as_ref().unwrap().0;
24132397

24142398
let mut link_upstream = |path: &Path| {
2415-
// We don't want to include the whole compiler-builtins crate (e.g., compiler-rt)
2416-
// regardless of the default because it'll get repeatedly linked anyway.
2417-
let path = fix_windows_verbatim_for_gcc(path);
2418-
if default_to_whole_archive(sess, crate_type, cmd)
2419-
&& codegen_results.crate_info.compiler_builtins != Some(cnum)
2420-
{
2421-
cmd.link_whole_rlib(&path);
2422-
} else {
2423-
cmd.link_rlib(&path);
2424-
}
2399+
cmd.link_rlib(&fix_windows_verbatim_for_gcc(path));
24252400
};
24262401

24272402
// See the comment above in `link_staticlib` and `link_rlib` for why if

compiler/rustc_codegen_ssa/src/back/linker.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,6 @@ pub trait Linker {
187187
fn no_crt_objects(&mut self);
188188
fn no_default_libraries(&mut self);
189189
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]);
190-
fn exported_symbol_means_used_symbol(&self) -> bool {
191-
true
192-
}
193190
fn subsystem(&mut self, subsystem: &str);
194191
fn group_start(&mut self);
195192
fn group_end(&mut self);
@@ -728,10 +725,6 @@ impl<'a> Linker for GccLinker<'a> {
728725
}
729726
}
730727

731-
fn exported_symbol_means_used_symbol(&self) -> bool {
732-
self.sess.target.is_like_windows || self.sess.target.is_like_osx
733-
}
734-
735728
fn subsystem(&mut self, subsystem: &str) {
736729
self.linker_arg("--subsystem");
737730
self.linker_arg(&subsystem);
@@ -1479,10 +1472,6 @@ impl<'a> Linker for L4Bender<'a> {
14791472
return;
14801473
}
14811474

1482-
fn exported_symbol_means_used_symbol(&self) -> bool {
1483-
false
1484-
}
1485-
14861475
fn subsystem(&mut self, subsystem: &str) {
14871476
self.cmd.arg(&format!("--subsystem {}", subsystem));
14881477
}
@@ -1564,8 +1553,8 @@ pub(crate) fn linked_symbols(
15641553
crate_type: CrateType,
15651554
) -> Vec<(String, SymbolExportKind)> {
15661555
match crate_type {
1567-
CrateType::Executable | CrateType::Cdylib => (),
1568-
CrateType::Staticlib | CrateType::ProcMacro | CrateType::Rlib | CrateType::Dylib => {
1556+
CrateType::Executable | CrateType::Cdylib | CrateType::Dylib => (),
1557+
CrateType::Staticlib | CrateType::ProcMacro | CrateType::Rlib => {
15691558
return Vec::new();
15701559
}
15711560
}

src/doc/rustc/src/command-line-arguments.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ to `/WHOLEARCHIVE` for `link.exe`, and to `-force_load` for `ld64`.
8080
The modifier does nothing for linkers that don't support it.
8181

8282
The default for this modifier is `-whole-archive`. \
83-
NOTE: The default may currently be different when building dylibs for some targets,
84-
but it is not guaranteed.
83+
NOTE: The default may currently be different in some cases for backward compatibility,
84+
but it is not guaranteed. If you need whole archive semantics use `+whole-archive` explicitly.
8585

8686
<a id="option-crate-type"></a>
8787
## `--crate-type`: a list of types of crates for the compiler to emit

0 commit comments

Comments
 (0)