Skip to content

Commit c7204f1

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 0e86aec commit c7204f1

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
@@ -2064,7 +2064,7 @@ fn linker_with_args<'a>(
20642064
// together with their respective upstream crates, and in their originally specified order.
20652065
// This may be slightly breaking due to our use of `--as-needed` and needs a crater run.
20662066
if sess.opts.unstable_opts.link_native_libraries {
2067-
add_upstream_native_libraries(cmd, sess, codegen_results);
2067+
add_upstream_native_libraries(cmd, sess, codegen_results, crate_type);
20682068
}
20692069

20702070
// Link with the import library generated for any raw-dylib functions.
@@ -2705,8 +2705,7 @@ fn add_upstream_rust_crates<'a>(
27052705
}
27062706
}
27072707

2708-
/// Link in all of our upstream crates' native dependencies. Remember that all of these upstream
2709-
/// native dependencies are all non-static dependencies. We've got two cases then:
2708+
/// Link in all of our upstream crates' native dependencies. We have two cases:
27102709
///
27112710
/// 1. The upstream crate is an rlib. In this case we *must* link in the native dependency because
27122711
/// the rlib is just an archive.
@@ -2724,6 +2723,7 @@ fn add_upstream_native_libraries(
27242723
cmd: &mut dyn Linker,
27252724
sess: &Session,
27262725
codegen_results: &CodegenResults,
2726+
crate_type: CrateType,
27272727
) {
27282728
let mut last = (None, NativeLibKind::Unspecified, None);
27292729
for &cnum in &codegen_results.crate_info.used_crates {
@@ -2748,7 +2748,19 @@ fn add_upstream_native_libraries(
27482748
NativeLibKind::Dylib { as_needed } => {
27492749
cmd.link_dylib(name, verbatim, as_needed.unwrap_or(true))
27502750
}
2751-
NativeLibKind::Unspecified => cmd.link_dylib(name, verbatim, true),
2751+
NativeLibKind::Unspecified => {
2752+
// On some targets, like Linux, linking a static executable inhibits using
2753+
// dylibs at all. Force native libraries to be static, even if for example
2754+
// an upstream rlib was originally linked against a native shared library.
2755+
if crate_type == config::CrateType::Executable
2756+
&& sess.crt_static(Some(crate_type))
2757+
&& !sess.target.options.crt_static_allows_dylibs
2758+
{
2759+
cmd.link_staticlib(name, verbatim)
2760+
} else {
2761+
cmd.link_dylib(name, verbatim, true)
2762+
}
2763+
},
27522764
NativeLibKind::Framework { as_needed } => {
27532765
cmd.link_framework(name, as_needed.unwrap_or(true))
27542766
}

0 commit comments

Comments
 (0)