Skip to content

Commit 741978c

Browse files
committed
Use static native libraries when linking static executables
On ELF targets like Linux, gcc/ld will create a dynamically-linked executable without warning, even when passed `-static`, when asked to link to a `.so`. Avoid this confusing and unintended behavior by always using the static version of libraries when trying to link static executables.
1 parent 257f795 commit 741978c

File tree

1 file changed

+16
-4
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+16
-4
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,7 +2024,7 @@ fn linker_with_args<'a>(
20242024
// together with their respective upstream crates, and in their originally specified order.
20252025
// This may be slightly breaking due to our use of `--as-needed` and needs a crater run.
20262026
if sess.opts.unstable_opts.link_native_libraries {
2027-
add_upstream_native_libraries(cmd, sess, codegen_results);
2027+
add_upstream_native_libraries(cmd, sess, codegen_results, crate_type);
20282028
}
20292029

20302030
// Link with the import library generated for any raw-dylib functions.
@@ -2665,8 +2665,7 @@ fn add_upstream_rust_crates<'a>(
26652665
}
26662666
}
26672667

2668-
/// Link in all of our upstream crates' native dependencies. Remember that all of these upstream
2669-
/// native dependencies are all non-static dependencies. We've got two cases then:
2668+
/// Link in all of our upstream crates' native dependencies. We have two cases:
26702669
///
26712670
/// 1. The upstream crate is an rlib. In this case we *must* link in the native dependency because
26722671
/// the rlib is just an archive.
@@ -2684,6 +2683,7 @@ fn add_upstream_native_libraries(
26842683
cmd: &mut dyn Linker,
26852684
sess: &Session,
26862685
codegen_results: &CodegenResults,
2686+
crate_type: CrateType,
26872687
) {
26882688
let mut last = (None, NativeLibKind::Unspecified, None);
26892689
for &cnum in &codegen_results.crate_info.used_crates {
@@ -2708,7 +2708,19 @@ fn add_upstream_native_libraries(
27082708
NativeLibKind::Dylib { as_needed } => {
27092709
cmd.link_dylib(name, verbatim, as_needed.unwrap_or(true))
27102710
}
2711-
NativeLibKind::Unspecified => cmd.link_dylib(name, verbatim, true),
2711+
NativeLibKind::Unspecified => {
2712+
// On some targets, like Linux, linking a static executable inhibits using
2713+
// dylibs at all. Force native libraries to be static, even if for example
2714+
// an upstream rlib was originally linked against a native shared library.
2715+
if crate_type == config::CrateType::Executable
2716+
&& sess.crt_static(Some(crate_type))
2717+
&& !sess.target.options.crt_static_allows_dylibs
2718+
{
2719+
cmd.link_staticlib(name, verbatim)
2720+
} else {
2721+
cmd.link_dylib(name, verbatim, true)
2722+
}
2723+
},
27122724
NativeLibKind::Framework { as_needed } => {
27132725
cmd.link_framework(name, as_needed.unwrap_or(true))
27142726
}

0 commit comments

Comments
 (0)