Skip to content

Commit f952c61

Browse files
committed
Require static native libraries when linking static executables
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. Fixes rust-lang#54243
1 parent cf81409 commit f952c61

File tree

1 file changed

+10
-3
lines changed
  • src/librustc_codegen_llvm/back

1 file changed

+10
-3
lines changed

src/librustc_codegen_llvm/back/link.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,8 +1414,8 @@ fn add_upstream_rust_crates(cmd: &mut dyn Linker,
14141414
}
14151415
}
14161416

1417-
// Link in all of our upstream crates' native dependencies. Remember that
1418-
// all of these upstream native dependencies are all non-static
1417+
// Link in all of our upstream crates' native dependencies. Remember that when
1418+
// linking libraries, these upstream native dependencies are all non-static
14191419
// dependencies. We've got two cases then:
14201420
//
14211421
// 1. The upstream crate is an rlib. In this case we *must* link in the
@@ -1459,7 +1459,14 @@ fn add_upstream_native_libraries(cmd: &mut dyn Linker,
14591459
continue
14601460
}
14611461
match lib.kind {
1462-
NativeLibraryKind::NativeUnknown => cmd.link_dylib(&name.as_str()),
1462+
NativeLibraryKind::NativeUnknown => {
1463+
// When creating executables, match library linkage to that of the executable.
1464+
if crate_type == config::CrateType::Executable && sess.crt_static() {
1465+
cmd.link_staticlib(&name.as_str())
1466+
} else {
1467+
cmd.link_dylib(&name.as_str())
1468+
}
1469+
},
14631470
NativeLibraryKind::NativeFramework => cmd.link_framework(&name.as_str()),
14641471
NativeLibraryKind::NativeStaticNobundle => {
14651472
// Link "static-nobundle" native libs only if the crate they originate from

0 commit comments

Comments
 (0)