Skip to content

Commit 12ceed0

Browse files
committed
Introduce target feature crt_static_allows_dylibs
Most UNIX-like platforms do not allow shared libraries to statically link their own libc, as libc expects to have consistent process-global state. On those platforms, when we do not have a shared libc available, we must not attempt to link dylibs or cdylibs. On Windows, however, it is expected to statically link the CRT into dynamic libraries. This feature is only relevant for targets that support both fully-static and fully-dynamic linkage, such as musl on Linux.
1 parent beb8abe commit 12ceed0

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/librustc_back/target/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ pub struct TargetOptions {
416416
/// ABIs are considered to be supported on all platforms and cannot be blacklisted.
417417
pub abi_blacklist: Vec<Abi>,
418418

419+
/// Whether or not linking dylibs to a static CRT is allowed.
420+
pub crt_static_allows_dylibs: bool,
419421
/// Whether or not the CRT is statically linked by default.
420422
pub crt_static_default: bool,
421423
/// Whether or not crt-static is respected by the compiler (or is a no-op).
@@ -480,6 +482,7 @@ impl Default for TargetOptions {
480482
max_atomic_width: None,
481483
panic_strategy: PanicStrategy::Unwind,
482484
abi_blacklist: vec![],
485+
crt_static_allows_dylibs: false,
483486
crt_static_default: false,
484487
crt_static_respected: false,
485488
stack_probes: false,
@@ -717,6 +720,7 @@ impl Target {
717720
key!(max_atomic_width, Option<u64>);
718721
key!(min_atomic_width, Option<u64>);
719722
try!(key!(panic_strategy, PanicStrategy));
723+
key!(crt_static_allows_dylibs, bool);
720724
key!(crt_static_default, bool);
721725
key!(crt_static_respected, bool);
722726
key!(stack_probes, bool);
@@ -906,6 +910,7 @@ impl ToJson for Target {
906910
target_option_val!(min_atomic_width);
907911
target_option_val!(max_atomic_width);
908912
target_option_val!(panic_strategy);
913+
target_option_val!(crt_static_allows_dylibs);
909914
target_option_val!(crt_static_default);
910915
target_option_val!(crt_static_respected);
911916
target_option_val!(stack_probes);

src/librustc_back/target/windows_msvc_base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub fn opts() -> TargetOptions {
6363
is_like_windows: true,
6464
is_like_msvc: true,
6565
pre_link_args: args,
66+
crt_static_allows_dylibs: true,
6667
crt_static_respected: true,
6768

6869
.. Default::default()

src/librustc_trans_utils/link.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,11 @@ pub fn invalid_output_for_target(sess: &Session,
123123
match (sess.target.target.options.dynamic_linking,
124124
sess.target.target.options.executables, crate_type) {
125125
(false, _, config::CrateTypeCdylib) |
126-
(false, _, config::CrateTypeProcMacro) |
127-
(false, _, config::CrateTypeDylib) => true,
126+
(false, _, config::CrateTypeDylib) |
127+
(false, _, config::CrateTypeProcMacro) => true,
128+
(true, _, config::CrateTypeCdylib) |
129+
(true, _, config::CrateTypeDylib) => sess.crt_static() &&
130+
!sess.target.target.options.crt_static_allows_dylibs,
128131
(_, false, config::CrateTypeExecutable) => true,
129132
_ => false
130133
}

0 commit comments

Comments
 (0)