1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
From 741978c5a73185b817cb2052413c0ef644dfad16 Mon Sep 17 00:00:00 2001
From: Samuel Holland <samuel@sholland.org>
Date: Fri, 8 Sep 2017 00:05:18 -0500
Subject: [PATCH 04/14] 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.
---
compiler/rustc_codegen_ssa/src/back/link.rs | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs
index 6cce95427463c..ef3fcef87e2a0 100644
--- a/compiler/rustc_codegen_ssa/src/back/link.rs
+++ b/compiler/rustc_codegen_ssa/src/back/link.rs
@@ -2024,7 +2024,7 @@ fn linker_with_args<'a>(
// together with their respective upstream crates, and in their originally specified order.
// This may be slightly breaking due to our use of `--as-needed` and needs a crater run.
if sess.opts.unstable_opts.link_native_libraries {
- add_upstream_native_libraries(cmd, sess, codegen_results);
+ add_upstream_native_libraries(cmd, sess, codegen_results, crate_type);
}
// Link with the import library generated for any raw-dylib functions.
@@ -2665,8 +2665,7 @@ fn add_upstream_rust_crates<'a>(
}
}
-/// Link in all of our upstream crates' native dependencies. Remember that all of these upstream
-/// native dependencies are all non-static dependencies. We've got two cases then:
+/// Link in all of our upstream crates' native dependencies. We have two cases:
///
/// 1. The upstream crate is an rlib. In this case we *must* link in the native dependency because
/// the rlib is just an archive.
@@ -2684,6 +2683,7 @@ fn add_upstream_native_libraries(
cmd: &mut dyn Linker,
sess: &Session,
codegen_results: &CodegenResults,
+ crate_type: CrateType,
) {
let mut last = (None, NativeLibKind::Unspecified, None);
for &cnum in &codegen_results.crate_info.used_crates {
@@ -2708,7 +2708,19 @@ fn add_upstream_native_libraries(
NativeLibKind::Dylib { as_needed } => {
cmd.link_dylib(name, verbatim, as_needed.unwrap_or(true))
}
- NativeLibKind::Unspecified => cmd.link_dylib(name, verbatim, true),
+ NativeLibKind::Unspecified => {
+ // On some targets, like Linux, linking a static executable inhibits using
+ // dylibs at all. Force native libraries to be static, even if for example
+ // an upstream rlib was originally linked against a native shared library.
+ if crate_type == config::CrateType::Executable
+ && sess.crt_static(Some(crate_type))
+ && !sess.target.options.crt_static_allows_dylibs
+ {
+ cmd.link_staticlib(name, verbatim)
+ } else {
+ cmd.link_dylib(name, verbatim, true)
+ }
+ },
NativeLibKind::Framework { as_needed } => {
cmd.link_framework(name, as_needed.unwrap_or(true))
}
|