summaryrefslogtreecommitdiff
path: root/user/rust/0004-Require-static-native-libraries-when-linking-static-.patch
blob: 7b737b792ba56022f4160d02c605422329223131 (plain) (blame)
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
From 8f2821b37da4fe2710e1bf7944765a5c2cf8d98c 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/18] Require 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.

Fixes #54243
---
 src/librustc_codegen_ssa/back/link.rs | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/librustc_codegen_ssa/back/link.rs b/src/librustc_codegen_ssa/back/link.rs
index 8fb0828285c..2d8a0b86076 100644
--- a/src/librustc_codegen_ssa/back/link.rs
+++ b/src/librustc_codegen_ssa/back/link.rs
@@ -1567,9 +1567,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(cmd: &mut dyn Linker,
     }
 }
 
-// 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.
@@ -1612,7 +1610,19 @@ pub fn add_upstream_native_libraries(cmd: &mut dyn Linker,
                 continue
             }
             match lib.kind {
-                NativeLibraryKind::NativeUnknown => cmd.link_dylib(&name.as_str()),
+                NativeLibraryKind::NativeUnknown => {
+                    // 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()
+                        && !sess.target.target.options.crt_static_allows_dylibs
+                    {
+                        cmd.link_staticlib(&name.as_str())
+                    } else {
+                        cmd.link_dylib(&name.as_str())
+                    }
+                },
                 NativeLibraryKind::NativeFramework => cmd.link_framework(&name.as_str()),
                 NativeLibraryKind::NativeStaticNobundle => {
                     // Link "static-nobundle" native libs only if the crate they originate from
-- 
2.21.0