Skip to content

Commit 2242107

Browse files
authored
Unrolled build for rust-lang#119162
Rollup merge of rust-lang#119162 - heiher:direct-access-external-data, r=petrochenkov Add unstable `-Z direct-access-external-data` cmdline flag for `rustc` The new flag has been described in the Major Change Proposal at rust-lang/compiler-team#707 Fixes rust-lang#118053
2 parents cfb42e5 + 06a4168 commit 2242107

File tree

8 files changed

+74
-10
lines changed

8 files changed

+74
-10
lines changed

Diff for: compiler/rustc_codegen_llvm/src/mono_item.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -123,25 +123,30 @@ impl CodegenCx<'_, '_> {
123123
return false;
124124
}
125125

126+
// Match clang by only supporting COFF and ELF for now.
127+
if self.tcx.sess.target.is_like_osx {
128+
return false;
129+
}
130+
131+
// With pie relocation model calls of functions defined in the translation
132+
// unit can use copy relocations.
133+
if self.tcx.sess.relocation_model() == RelocModel::Pie && !is_declaration {
134+
return true;
135+
}
136+
126137
// Thread-local variables generally don't support copy relocations.
127138
let is_thread_local_var = llvm::LLVMIsAGlobalVariable(llval)
128139
.is_some_and(|v| llvm::LLVMIsThreadLocal(v) == llvm::True);
129140
if is_thread_local_var {
130141
return false;
131142
}
132143

133-
// Match clang by only supporting COFF and ELF for now.
134-
if self.tcx.sess.target.is_like_osx {
135-
return false;
144+
// Respect the direct-access-external-data to override default behavior if present.
145+
if let Some(direct) = self.tcx.sess.direct_access_external_data() {
146+
return direct;
136147
}
137148

138149
// Static relocation model should force copy relocations everywhere.
139-
if self.tcx.sess.relocation_model() == RelocModel::Static {
140-
return true;
141-
}
142-
143-
// With pie relocation model calls of functions defined in the translation
144-
// unit can use copy relocations.
145-
self.tcx.sess.relocation_model() == RelocModel::Pie && !is_declaration
150+
self.tcx.sess.relocation_model() == RelocModel::Static
146151
}
147152
}

Diff for: compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ fn test_unstable_options_tracking_hash() {
749749
tracked!(debug_macros, true);
750750
tracked!(default_hidden_visibility, Some(true));
751751
tracked!(dep_info_omit_d_target, true);
752+
tracked!(direct_access_external_data, Some(true));
752753
tracked!(dual_proc_macros, true);
753754
tracked!(dwarf_version, Some(5));
754755
tracked!(emit_thin_lto, false);

Diff for: compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,8 @@ options! {
15721572
dep_info_omit_d_target: bool = (false, parse_bool, [TRACKED],
15731573
"in dep-info output, omit targets for tracking dependencies of the dep-info files \
15741574
themselves (default: no)"),
1575+
direct_access_external_data: Option<bool> = (None, parse_opt_bool, [TRACKED],
1576+
"Direct or use GOT indirect to reference external data symbols"),
15751577
dual_proc_macros: bool = (false, parse_bool, [TRACKED],
15761578
"load proc macros for both target and host, but only link to the target (default: no)"),
15771579
dump_dep_graph: bool = (false, parse_bool, [UNTRACKED],

Diff for: compiler/rustc_session/src/session.rs

+7
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,13 @@ impl Session {
767767
self.opts.unstable_opts.tls_model.unwrap_or(self.target.tls_model)
768768
}
769769

770+
pub fn direct_access_external_data(&self) -> Option<bool> {
771+
self.opts
772+
.unstable_opts
773+
.direct_access_external_data
774+
.or(self.target.direct_access_external_data)
775+
}
776+
770777
pub fn split_debuginfo(&self) -> SplitDebuginfo {
771778
self.opts.cg.split_debuginfo.unwrap_or(self.target.split_debuginfo)
772779
}

Diff for: compiler/rustc_target/src/spec/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,8 @@ pub struct TargetOptions {
18861886
/// passed, and cannot be disabled even via `-C`. Corresponds to `llc
18871887
/// -mattr=$features`.
18881888
pub features: StaticCow<str>,
1889+
/// Direct or use GOT indirect to reference external data symbols
1890+
pub direct_access_external_data: Option<bool>,
18891891
/// Whether dynamic linking is available on this target. Defaults to false.
18901892
pub dynamic_linking: bool,
18911893
/// Whether dynamic linking can export TLS globals. Defaults to true.
@@ -2280,6 +2282,7 @@ impl Default for TargetOptions {
22802282
asm_args: cvs![],
22812283
cpu: "generic".into(),
22822284
features: "".into(),
2285+
direct_access_external_data: None,
22832286
dynamic_linking: false,
22842287
dll_tls_export: true,
22852288
only_cdylib: false,
@@ -2579,6 +2582,12 @@ impl Target {
25792582
base.$key_name = s as u32;
25802583
}
25812584
} );
2585+
($key_name:ident, Option<bool>) => ( {
2586+
let name = (stringify!($key_name)).replace("_", "-");
2587+
if let Some(s) = obj.remove(&name).and_then(|b| b.as_bool()) {
2588+
base.$key_name = Some(s);
2589+
}
2590+
} );
25822591
($key_name:ident, Option<u64>) => ( {
25832592
let name = (stringify!($key_name)).replace("_", "-");
25842593
if let Some(s) = obj.remove(&name).and_then(|b| b.as_u64()) {
@@ -3007,6 +3016,7 @@ impl Target {
30073016
key!(cpu);
30083017
key!(features);
30093018
key!(dynamic_linking, bool);
3019+
key!(direct_access_external_data, Option<bool>);
30103020
key!(dll_tls_export, bool);
30113021
key!(only_cdylib, bool);
30123022
key!(executables, bool);
@@ -3261,6 +3271,7 @@ impl ToJson for Target {
32613271
target_option_val!(cpu);
32623272
target_option_val!(features);
32633273
target_option_val!(dynamic_linking);
3274+
target_option_val!(direct_access_external_data);
32643275
target_option_val!(dll_tls_export);
32653276
target_option_val!(only_cdylib);
32663277
target_option_val!(executables);

Diff for: compiler/rustc_target/src/spec/targets/loongarch64_unknown_linux_gnu.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub fn target() -> Target {
1212
features: "+f,+d".into(),
1313
llvm_abiname: "lp64d".into(),
1414
max_atomic_width: Some(64),
15+
direct_access_external_data: Some(false),
1516
..base::linux_gnu::opts()
1617
},
1718
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# `direct_access_external_data`
2+
3+
The tracking issue for this feature is: https://github.com/rust-lang/compiler-team/issues/707
4+
5+
------------------------
6+
7+
Option `-Z direct-access-external-data` controls how to access symbols of
8+
external data.
9+
10+
Supported values for this option are:
11+
12+
- `yes` - Don't use GOT indirection to reference external data symbols.
13+
- `no` - Use GOT indirection to reference external data symbols.
14+
15+
If the option is not explicitly specified, different targets have different
16+
default values.

Diff for: tests/codegen/direct-access-external-data.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// only-loongarch64-unknown-linux-gnu
2+
3+
// revisions: DEFAULT DIRECT INDIRECT
4+
// [DEFAULT] compile-flags: -C relocation-model=static
5+
// [DIRECT] compile-flags: -C relocation-model=static -Z direct-access-external-data=yes
6+
// [INDIRECT] compile-flags: -C relocation-model=static -Z direct-access-external-data=no
7+
8+
#![crate_type = "rlib"]
9+
10+
// DEFAULT: @VAR = external {{.*}} global i32
11+
// DIRECT: @VAR = external dso_local {{.*}} global i32
12+
// INDIRECT: @VAR = external {{.*}} global i32
13+
14+
extern "C" {
15+
static VAR: i32;
16+
}
17+
18+
#[no_mangle]
19+
pub fn get() -> i32 {
20+
unsafe { VAR }
21+
}

0 commit comments

Comments
 (0)