Skip to content

Commit 6ad0952

Browse files
committed
Auto merge of #131634 - davidlattimore:lld-protected, r=<try>
Use protected visibility when LLD feature is enabled and enable it when building rustc rust-lang/compiler-team#782 I wasn't sure about having two commits in a PR, but I figured, at least initially it might make sense to discuss these commits together. Happy to squash, or move the second commit to a separate PR. I contemplated trying to enable protected visibility for more cases when LLD will be used other than just `-Zlinker-features=+lld`, but that would be more a complex change that probably still wouldn't cover all cases when LLD is used, so went with the simplest option of just checking if the linker-feature is enabled. r? lqd
2 parents 2aa26d8 + 1e1bf2b commit 6ad0952

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

compiler/rustc_session/src/session.rs

+11-2
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/bootstrap/src/utils/helpers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ pub fn linker_flags(
428428
) -> Vec<String> {
429429
let mut args = vec![];
430430
if !builder.is_lld_direct_linker(target) && builder.config.lld_mode.is_used() {
431-
args.push(String::from("-Clink-arg=-fuse-ld=lld"));
431+
args.push(String::from("-Zlinker-features=+lld"));
432432

433433
if matches!(lld_threads, LldThreads::No) {
434434
args.push(format!(

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

+4-1
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

+7-3
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)