Skip to content

Commit 06a4168

Browse files
committed
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
1 parent 9480767 commit 06a4168

File tree

8 files changed

+74
-10
lines changed

8 files changed

+74
-10
lines changed

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
}

compiler/rustc_interface/src/tests.rs

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

compiler/rustc_session/src/options.rs

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

compiler/rustc_session/src/session.rs

+7
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,13 @@ impl Session {
793793
self.opts.unstable_opts.tls_model.unwrap_or(self.target.tls_model)
794794
}
795795

796+
pub fn direct_access_external_data(&self) -> Option<bool> {
797+
self.opts
798+
.unstable_opts
799+
.direct_access_external_data
800+
.or(self.target.direct_access_external_data)
801+
}
802+
796803
pub fn split_debuginfo(&self) -> SplitDebuginfo {
797804
self.opts.cg.split_debuginfo.unwrap_or(self.target.split_debuginfo)
798805
}

compiler/rustc_target/src/spec/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,8 @@ pub struct TargetOptions {
18851885
/// passed, and cannot be disabled even via `-C`. Corresponds to `llc
18861886
/// -mattr=$features`.
18871887
pub features: StaticCow<str>,
1888+
/// Direct or use GOT indirect to reference external data symbols
1889+
pub direct_access_external_data: Option<bool>,
18881890
/// Whether dynamic linking is available on this target. Defaults to false.
18891891
pub dynamic_linking: bool,
18901892
/// Whether dynamic linking can export TLS globals. Defaults to true.
@@ -2279,6 +2281,7 @@ impl Default for TargetOptions {
22792281
asm_args: cvs![],
22802282
cpu: "generic".into(),
22812283
features: "".into(),
2284+
direct_access_external_data: None,
22822285
dynamic_linking: false,
22832286
dll_tls_export: true,
22842287
only_cdylib: false,
@@ -2575,6 +2578,12 @@ impl Target {
25752578
base.$key_name = s as u32;
25762579
}
25772580
} );
2581+
($key_name:ident, Option<bool>) => ( {
2582+
let name = (stringify!($key_name)).replace("_", "-");
2583+
if let Some(s) = obj.remove(&name).and_then(|b| b.as_bool()) {
2584+
base.$key_name = Some(s);
2585+
}
2586+
} );
25782587
($key_name:ident, Option<u64>) => ( {
25792588
let name = (stringify!($key_name)).replace("_", "-");
25802589
if let Some(s) = obj.remove(&name).and_then(|b| b.as_u64()) {
@@ -3003,6 +3012,7 @@ impl Target {
30033012
key!(cpu);
30043013
key!(features);
30053014
key!(dynamic_linking, bool);
3015+
key!(direct_access_external_data, Option<bool>);
30063016
key!(dll_tls_export, bool);
30073017
key!(only_cdylib, bool);
30083018
key!(executables, bool);
@@ -3257,6 +3267,7 @@ impl ToJson for Target {
32573267
target_option_val!(cpu);
32583268
target_option_val!(features);
32593269
target_option_val!(dynamic_linking);
3270+
target_option_val!(direct_access_external_data);
32603271
target_option_val!(dll_tls_export);
32613272
target_option_val!(only_cdylib);
32623273
target_option_val!(executables);

compiler/rustc_target/src/spec/targets/loongarch64_unknown_linux_gnu.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub fn target() -> Target {
1111
features: "+f,+d".into(),
1212
llvm_abiname: "lp64d".into(),
1313
max_atomic_width: Some(64),
14+
direct_access_external_data: Some(false),
1415
..base::linux_gnu::opts()
1516
},
1617
}
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.
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)