Skip to content

Commit 02e9525

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 de2dcc5 commit 02e9525

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
@@ -1992,7 +1992,7 @@ fn linker_with_args<'a>(
19921992
// external build system already has the native dependencies defined, and it
19931993
// will provide them to the linker itself.
19941994
if sess.opts.unstable_opts.link_native_libraries {
1995-
add_upstream_native_libraries(cmd, sess, codegen_results);
1995+
add_upstream_native_libraries(cmd, sess, codegen_results, crate_type);
19961996
}
19971997

19981998
// Link with the import library generated for any raw-dylib functions.
@@ -2593,8 +2593,7 @@ fn add_upstream_rust_crates<'a>(
25932593
}
25942594
}
25952595

2596-
/// Link in all of our upstream crates' native dependencies. Remember that all of these upstream
2597-
/// native dependencies are all non-static dependencies. We've got two cases then:
2596+
/// Link in all of our upstream crates' native dependencies. We have two cases:
25982597
///
25992598
/// 1. The upstream crate is an rlib. In this case we *must* link in the native dependency because
26002599
/// the rlib is just an archive.
@@ -2612,6 +2611,7 @@ fn add_upstream_native_libraries(
26122611
cmd: &mut dyn Linker,
26132612
sess: &Session,
26142613
codegen_results: &CodegenResults,
2614+
crate_type: CrateType,
26152615
) {
26162616
let mut last = (None, NativeLibKind::Unspecified, None);
26172617
for &cnum in &codegen_results.crate_info.used_crates {
@@ -2636,7 +2636,19 @@ fn add_upstream_native_libraries(
26362636
NativeLibKind::Dylib { as_needed } => {
26372637
cmd.link_dylib(name, verbatim, as_needed.unwrap_or(true))
26382638
}
2639-
NativeLibKind::Unspecified => cmd.link_dylib(name, verbatim, true),
2639+
NativeLibKind::Unspecified => {
2640+
// On some targets, like Linux, linking a static executable inhibits using
2641+
// dylibs at all. Force native libraries to be static, even if for example
2642+
// an upstream rlib was originally linked against a native shared library.
2643+
if crate_type == config::CrateType::Executable
2644+
&& sess.crt_static(Some(crate_type))
2645+
&& !sess.target.options.crt_static_allows_dylibs
2646+
{
2647+
cmd.link_staticlib(name, verbatim)
2648+
} else {
2649+
cmd.link_dylib(name, verbatim, true)
2650+
}
2651+
},
26402652
NativeLibKind::Framework { as_needed } => {
26412653
cmd.link_framework(name, as_needed.unwrap_or(true))
26422654
}

0 commit comments

Comments
 (0)