Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7bb27f8

Browse files
Use protected visibility by default when lld linker feature is enabled
1 parent 6b9676b commit 7bb27f8

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

compiler/rustc_session/src/session.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_span::source_map::{FilePathMapping, SourceMap};
3030
use rustc_span::{FileNameDisplayPreference, RealFileName, Span, Symbol};
3131
use rustc_target::asm::InlineAsmArch;
3232
use rustc_target::spec::{
33-
CodeModel, DebuginfoKind, PanicStrategy, RelocModel, RelroLevel, SanitizerSet,
33+
CodeModel, DebuginfoKind, LinkerFeatures, PanicStrategy, RelocModel, RelroLevel, SanitizerSet,
3434
SmallDataThresholdSupport, SplitDebuginfo, StackProtector, SymbolVisibility, Target,
3535
TargetTriple, TlsModel,
3636
};
@@ -622,11 +622,20 @@ impl Session {
622622

623623
/// Returns the default symbol visibility.
624624
pub fn default_visibility(&self) -> SymbolVisibility {
625+
// For now, we default to using protected only if the LLD linker features is enabled. This
626+
// is to avoid link errors that are likely if using protected visibility with GNU ld < 2.40.
627+
let fallback =
628+
if self.opts.unstable_opts.linker_features.enabled.contains(LinkerFeatures::LLD) {
629+
SymbolVisibility::Protected
630+
} else {
631+
SymbolVisibility::Interposable
632+
};
633+
625634
self.opts
626635
.unstable_opts
627636
.default_visibility
628637
.or(self.target.options.default_visibility)
629-
.unwrap_or(SymbolVisibility::Interposable)
638+
.unwrap_or(fallback)
630639
}
631640
}
632641

src/doc/unstable-book/src/compiler-flags/default-visibility.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ setting.
1010

1111
This option only affects building of shared objects and should have no effect on executables.
1212

13-
Visibility an be set to one of three options:
13+
Visibility can be set to one of three options:
1414

1515
* protected
1616
* hidden
1717
* interposable
1818

19+
The default value for this flag is `interposable` unless `-Zlinker-features=+lld` is specified, in
20+
which case the default is `protected`.
21+
1922
## Hidden visibility
2023

2124
Using `-Zdefault-visibility=hidden` is roughly equivalent to Clang's

tests/codegen/default-visibility.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
// also https://github.com/rust-lang/rust/issues/73295 and
44
// https://github.com/rust-lang/rust/issues/37530.
55

6-
//@ revisions:DEFAULT HIDDEN PROTECTED INTERPOSABLE
6+
//@ revisions:NO_LLD LLD HIDDEN PROTECTED INTERPOSABLE
77
//@[HIDDEN] compile-flags: -Zdefault-visibility=hidden
88
//@[PROTECTED] compile-flags: -Zdefault-visibility=protected
99
//@[INTERPOSABLE] compile-flags: -Zdefault-visibility=interposable
10+
//@[NO_LLD] compile-flags: -Zlinker-features=-lld
11+
//@[LLD] compile-flags: -Zlinker-features=+lld
1012

1113
// The test scenario is specifically about visibility of symbols exported out of dynamically linked
1214
// libraries.
@@ -30,7 +32,8 @@ pub static tested_symbol: [u8; 6] = *b"foobar";
3032
// HIDDEN: @{{.*}}default_visibility{{.*}}tested_symbol{{.*}} = hidden constant
3133
// PROTECTED: @{{.*}}default_visibility{{.*}}tested_symbol{{.*}} = protected constant
3234
// INTERPOSABLE: @{{.*}}default_visibility{{.*}}tested_symbol{{.*}} = constant
33-
// DEFAULT: @{{.*}}default_visibility{{.*}}tested_symbol{{.*}} = constant
35+
// NO_LLD: @{{.*}}default_visibility{{.*}}tested_symbol{{.*}} = constant
36+
// LLD: @{{.*}}default_visibility{{.*}}tested_symbol{{.*}} = protected constant
3437

3538
pub fn do_memcmp(left: &[u8], right: &[u8]) -> i32 {
3639
left.cmp(right) as i32
@@ -46,4 +49,5 @@ pub fn do_memcmp(left: &[u8], right: &[u8]) -> i32 {
4649
// HIDDEN: declare i32 @memcmp
4750
// PROTECTED: declare i32 @memcmp
4851
// INTERPOSABLE: declare i32 @memcmp
49-
// DEFAULT: declare i32 @memcmp
52+
// NO_LLD: declare i32 @memcmp
53+
// LLD: declare i32 @memcmp

0 commit comments

Comments
 (0)