Skip to content

Commit 82c5732

Browse files
committed
Auto merge of #113966 - lu-zero:relocation-model-in-cfg, r=bjorn3
Add the relocation_model to the cfg This way is possible to write inline assembly code aware of it.
2 parents 9c699a4 + c0394c8 commit 82c5732

File tree

8 files changed

+86
-12
lines changed

8 files changed

+86
-12
lines changed

compiler/rustc_feature/src/active.rs

+2
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ declare_features! (
352352
(active, c_variadic, "1.34.0", Some(44930), None),
353353
/// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.
354354
(active, cfg_overflow_checks, "1.71.0", Some(111466), None),
355+
/// Provides the relocation model information as cfg entry
356+
(active, cfg_relocation_model, "CURRENT_RUSTC_VERSION", Some(114929), None),
355357
/// Allows the use of `#[cfg(sanitize = "option")]`; set when -Zsanitizer is used.
356358
(active, cfg_sanitize, "1.41.0", Some(39699), None),
357359
/// Allows `cfg(target_abi = "...")`.

compiler/rustc_feature/src/builtin_attrs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const GATED_CFGS: &[GatedCfg] = &[
3535
(sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
3636
(sym::sanitize, sym::cfg_sanitize, cfg_fn!(cfg_sanitize)),
3737
(sym::version, sym::cfg_version, cfg_fn!(cfg_version)),
38+
(sym::relocation_model, sym::cfg_relocation_model, cfg_fn!(cfg_relocation_model)),
3839
];
3940

4041
/// Find a gated cfg determined by the `pred`icate which is given the cfg's name.

compiler/rustc_session/src/config.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{EarlyErrorHandler, Session};
1212
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1313
use rustc_data_structures::stable_hasher::{StableOrd, ToStableHashKey};
1414
use rustc_target::abi::Align;
15-
use rustc_target::spec::{PanicStrategy, SanitizerSet, SplitDebuginfo};
15+
use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet, SplitDebuginfo};
1616
use rustc_target::spec::{Target, TargetTriple, TargetWarnings, TARGETS};
1717

1818
use crate::parse::{CrateCheckConfig, CrateConfig};
@@ -1193,6 +1193,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
11931193
let os = &sess.target.os;
11941194
let env = &sess.target.env;
11951195
let abi = &sess.target.abi;
1196+
let relocation_model = sess.target.relocation_model.desc_symbol();
11961197
let vendor = &sess.target.vendor;
11971198
let min_atomic_width = sess.target.min_atomic_width();
11981199
let max_atomic_width = sess.target.max_atomic_width();
@@ -1218,6 +1219,9 @@ fn default_configuration(sess: &Session) -> CrateConfig {
12181219
ret.insert((sym::target_pointer_width, Some(Symbol::intern(&wordsz))));
12191220
ret.insert((sym::target_env, Some(Symbol::intern(env))));
12201221
ret.insert((sym::target_abi, Some(Symbol::intern(abi))));
1222+
if sess.is_nightly_build() {
1223+
ret.insert((sym::relocation_model, Some(relocation_model)));
1224+
}
12211225
ret.insert((sym::target_vendor, Some(Symbol::intern(vendor))));
12221226
if sess.target.has_thread_local {
12231227
ret.insert((sym::target_thread_local, None));
@@ -1415,6 +1419,8 @@ impl CrateCheckConfig {
14151419
.into_iter()
14161420
.map(|sanitizer| Symbol::intern(sanitizer.as_str().unwrap()));
14171421

1422+
let relocation_model_values = RelocModel::all();
1423+
14181424
// Unknown possible values:
14191425
// - `feature`
14201426
// - `target_feature`
@@ -1453,6 +1459,10 @@ impl CrateCheckConfig {
14531459
.entry(sym::target_has_atomic_equal_alignment)
14541460
.or_insert_with(no_values)
14551461
.extend(atomic_values);
1462+
self.expecteds
1463+
.entry(sym::relocation_model)
1464+
.or_insert_with(empty_values)
1465+
.extend(relocation_model_values);
14561466

14571467
// Target specific values
14581468
{

compiler/rustc_span/src/symbol.rs

+8
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ symbols! {
468468
cfg_hide,
469469
cfg_overflow_checks,
470470
cfg_panic,
471+
cfg_relocation_model,
471472
cfg_sanitize,
472473
cfg_target_abi,
473474
cfg_target_compact,
@@ -661,6 +662,7 @@ symbols! {
661662
dyn_metadata,
662663
dyn_star,
663664
dyn_trait,
665+
dynamic_no_pic: "dynamic-no-pic",
664666
e,
665667
edition_panic,
666668
effects,
@@ -1115,6 +1117,8 @@ symbols! {
11151117
path,
11161118
pattern_parentheses,
11171119
phantom_data,
1120+
pic,
1121+
pie,
11181122
pin,
11191123
platform_intrinsics,
11201124
plugin,
@@ -1223,6 +1227,7 @@ symbols! {
12231227
register_tool,
12241228
relaxed_adts,
12251229
relaxed_struct_unsize,
1230+
relocation_model,
12261231
rem,
12271232
rem_assign,
12281233
repr,
@@ -1243,6 +1248,8 @@ symbols! {
12431248
rintf64,
12441249
riscv_target_feature,
12451250
rlib,
1251+
ropi,
1252+
ropi_rwpi: "ropi-rwpi",
12461253
rotate_left,
12471254
rotate_right,
12481255
roundevenf32,
@@ -1354,6 +1361,7 @@ symbols! {
13541361
rustdoc_missing_doc_code_examples,
13551362
rustfmt,
13561363
rvalue_static_promotion,
1364+
rwpi,
13571365
s,
13581366
safety,
13591367
sanitize,

compiler/rustc_target/src/spec/mod.rs

+39-11
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::spec::crt_objects::{CrtObjects, LinkSelfContainedDefault};
4242
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4343
use rustc_fs_util::try_canonicalize;
4444
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
45-
use rustc_span::symbol::{sym, Symbol};
45+
use rustc_span::symbol::{kw, sym, Symbol};
4646
use serde_json::Value;
4747
use std::borrow::Cow;
4848
use std::collections::BTreeMap;
@@ -655,6 +655,43 @@ pub enum RelocModel {
655655
RopiRwpi,
656656
}
657657

658+
impl RelocModel {
659+
pub fn desc(&self) -> &str {
660+
match *self {
661+
RelocModel::Static => "static",
662+
RelocModel::Pic => "pic",
663+
RelocModel::Pie => "pie",
664+
RelocModel::DynamicNoPic => "dynamic-no-pic",
665+
RelocModel::Ropi => "ropi",
666+
RelocModel::Rwpi => "rwpi",
667+
RelocModel::RopiRwpi => "ropi-rwpi",
668+
}
669+
}
670+
pub const fn desc_symbol(&self) -> Symbol {
671+
match *self {
672+
RelocModel::Static => kw::Static,
673+
RelocModel::Pic => sym::pic,
674+
RelocModel::Pie => sym::pie,
675+
RelocModel::DynamicNoPic => sym::dynamic_no_pic,
676+
RelocModel::Ropi => sym::ropi,
677+
RelocModel::Rwpi => sym::rwpi,
678+
RelocModel::RopiRwpi => sym::ropi_rwpi,
679+
}
680+
}
681+
682+
pub const fn all() -> [Symbol; 7] {
683+
[
684+
RelocModel::Static.desc_symbol(),
685+
RelocModel::Pic.desc_symbol(),
686+
RelocModel::Pie.desc_symbol(),
687+
RelocModel::DynamicNoPic.desc_symbol(),
688+
RelocModel::Ropi.desc_symbol(),
689+
RelocModel::Rwpi.desc_symbol(),
690+
RelocModel::RopiRwpi.desc_symbol(),
691+
]
692+
}
693+
}
694+
658695
impl FromStr for RelocModel {
659696
type Err = ();
660697

@@ -674,16 +711,7 @@ impl FromStr for RelocModel {
674711

675712
impl ToJson for RelocModel {
676713
fn to_json(&self) -> Json {
677-
match *self {
678-
RelocModel::Static => "static",
679-
RelocModel::Pic => "pic",
680-
RelocModel::Pie => "pie",
681-
RelocModel::DynamicNoPic => "dynamic-no-pic",
682-
RelocModel::Ropi => "ropi",
683-
RelocModel::Rwpi => "rwpi",
684-
RelocModel::RopiRwpi => "ropi-rwpi",
685-
}
686-
.to_json()
714+
self.desc().to_json()
687715
}
688716
}
689717

tests/ui/abi/relocation_model_pic.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-pass
2+
// compile-flags: -C relocation-model=pic
3+
// ignore-emscripten no pic
4+
// ignore-wasm
5+
6+
#![feature(cfg_relocation_model)]
7+
8+
#[cfg(relocation_model = "pic")]
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[cfg(relocation_model = "pic")] //~ ERROR
2+
fn _foo() {}
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: `cfg(relocation_model)` is experimental and subject to change
2+
--> $DIR/feature-gate-cfg-relocation-model.rs:1:7
3+
|
4+
LL | #[cfg(relocation_model = "pic")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #114929 <https://github.com/rust-lang/rust/issues/114929> for more information
8+
= help: add `#![feature(cfg_relocation_model)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)