Add the relocation_model to the cfg

This way is possible to write inline assembly code aware of it.
This commit is contained in:
Luca Barbato 2023-07-22 17:16:18 +02:00
parent 2ceed0b6cb
commit c0394c8ac0
8 changed files with 86 additions and 12 deletions

View file

@ -352,6 +352,8 @@ pub fn set(&self, features: &mut Features, span: Span) {
(active, c_variadic, "1.34.0", Some(44930), None),
/// Allows the use of `#[cfg(overflow_checks)` to check if integer overflow behaviour.
(active, cfg_overflow_checks, "1.71.0", Some(111466), None),
/// Provides the relocation model information as cfg entry
(active, cfg_relocation_model, "CURRENT_RUSTC_VERSION", Some(114929), None),
/// Allows the use of `#[cfg(sanitize = "option")]`; set when -Zsanitizer is used.
(active, cfg_sanitize, "1.41.0", Some(39699), None),
/// Allows `cfg(target_abi = "...")`.

View file

@ -35,6 +35,7 @@ macro_rules! cfg_fn {
(sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
(sym::sanitize, sym::cfg_sanitize, cfg_fn!(cfg_sanitize)),
(sym::version, sym::cfg_version, cfg_fn!(cfg_version)),
(sym::relocation_model, sym::cfg_relocation_model, cfg_fn!(cfg_relocation_model)),
];
/// Find a gated cfg determined by the `pred`icate which is given the cfg's name.

View file

@ -12,7 +12,7 @@
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stable_hasher::{StableOrd, ToStableHashKey};
use rustc_target::abi::Align;
use rustc_target::spec::{PanicStrategy, SanitizerSet, SplitDebuginfo};
use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet, SplitDebuginfo};
use rustc_target::spec::{Target, TargetTriple, TargetWarnings, TARGETS};
use crate::parse::{CrateCheckConfig, CrateConfig};
@ -1193,6 +1193,7 @@ fn default_configuration(sess: &Session) -> CrateConfig {
let os = &sess.target.os;
let env = &sess.target.env;
let abi = &sess.target.abi;
let relocation_model = sess.target.relocation_model.desc_symbol();
let vendor = &sess.target.vendor;
let min_atomic_width = sess.target.min_atomic_width();
let max_atomic_width = sess.target.max_atomic_width();
@ -1218,6 +1219,9 @@ fn default_configuration(sess: &Session) -> CrateConfig {
ret.insert((sym::target_pointer_width, Some(Symbol::intern(&wordsz))));
ret.insert((sym::target_env, Some(Symbol::intern(env))));
ret.insert((sym::target_abi, Some(Symbol::intern(abi))));
if sess.is_nightly_build() {
ret.insert((sym::relocation_model, Some(relocation_model)));
}
ret.insert((sym::target_vendor, Some(Symbol::intern(vendor))));
if sess.target.has_thread_local {
ret.insert((sym::target_thread_local, None));
@ -1415,6 +1419,8 @@ pub fn fill_well_known(&mut self, current_target: &Target) {
.into_iter()
.map(|sanitizer| Symbol::intern(sanitizer.as_str().unwrap()));
let relocation_model_values = RelocModel::all();
// Unknown possible values:
// - `feature`
// - `target_feature`
@ -1453,6 +1459,10 @@ pub fn fill_well_known(&mut self, current_target: &Target) {
.entry(sym::target_has_atomic_equal_alignment)
.or_insert_with(no_values)
.extend(atomic_values);
self.expecteds
.entry(sym::relocation_model)
.or_insert_with(empty_values)
.extend(relocation_model_values);
// Target specific values
{

View file

@ -468,6 +468,7 @@
cfg_hide,
cfg_overflow_checks,
cfg_panic,
cfg_relocation_model,
cfg_sanitize,
cfg_target_abi,
cfg_target_compact,
@ -661,6 +662,7 @@
dyn_metadata,
dyn_star,
dyn_trait,
dynamic_no_pic: "dynamic-no-pic",
e,
edition_panic,
effects,
@ -1115,6 +1117,8 @@
path,
pattern_parentheses,
phantom_data,
pic,
pie,
pin,
platform_intrinsics,
plugin,
@ -1223,6 +1227,7 @@
register_tool,
relaxed_adts,
relaxed_struct_unsize,
relocation_model,
rem,
rem_assign,
repr,
@ -1243,6 +1248,8 @@
rintf64,
riscv_target_feature,
rlib,
ropi,
ropi_rwpi: "ropi-rwpi",
rotate_left,
rotate_right,
roundevenf32,
@ -1354,6 +1361,7 @@
rustdoc_missing_doc_code_examples,
rustfmt,
rvalue_static_promotion,
rwpi,
s,
safety,
sanitize,

View file

@ -42,7 +42,7 @@
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_fs_util::try_canonicalize;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_span::symbol::{sym, Symbol};
use rustc_span::symbol::{kw, sym, Symbol};
use serde_json::Value;
use std::borrow::Cow;
use std::collections::BTreeMap;
@ -655,6 +655,43 @@ pub enum RelocModel {
RopiRwpi,
}
impl RelocModel {
pub fn desc(&self) -> &str {
match *self {
RelocModel::Static => "static",
RelocModel::Pic => "pic",
RelocModel::Pie => "pie",
RelocModel::DynamicNoPic => "dynamic-no-pic",
RelocModel::Ropi => "ropi",
RelocModel::Rwpi => "rwpi",
RelocModel::RopiRwpi => "ropi-rwpi",
}
}
pub const fn desc_symbol(&self) -> Symbol {
match *self {
RelocModel::Static => kw::Static,
RelocModel::Pic => sym::pic,
RelocModel::Pie => sym::pie,
RelocModel::DynamicNoPic => sym::dynamic_no_pic,
RelocModel::Ropi => sym::ropi,
RelocModel::Rwpi => sym::rwpi,
RelocModel::RopiRwpi => sym::ropi_rwpi,
}
}
pub const fn all() -> [Symbol; 7] {
[
RelocModel::Static.desc_symbol(),
RelocModel::Pic.desc_symbol(),
RelocModel::Pie.desc_symbol(),
RelocModel::DynamicNoPic.desc_symbol(),
RelocModel::Ropi.desc_symbol(),
RelocModel::Rwpi.desc_symbol(),
RelocModel::RopiRwpi.desc_symbol(),
]
}
}
impl FromStr for RelocModel {
type Err = ();
@ -674,16 +711,7 @@ fn from_str(s: &str) -> Result<RelocModel, ()> {
impl ToJson for RelocModel {
fn to_json(&self) -> Json {
match *self {
RelocModel::Static => "static",
RelocModel::Pic => "pic",
RelocModel::Pie => "pie",
RelocModel::DynamicNoPic => "dynamic-no-pic",
RelocModel::Ropi => "ropi",
RelocModel::Rwpi => "rwpi",
RelocModel::RopiRwpi => "ropi-rwpi",
}
.to_json()
self.desc().to_json()
}
}

View file

@ -0,0 +1,9 @@
// run-pass
// compile-flags: -C relocation-model=pic
// ignore-emscripten no pic
// ignore-wasm
#![feature(cfg_relocation_model)]
#[cfg(relocation_model = "pic")]
fn main() {}

View file

@ -0,0 +1,4 @@
#[cfg(relocation_model = "pic")] //~ ERROR
fn _foo() {}
fn main() {}

View file

@ -0,0 +1,12 @@
error[E0658]: `cfg(relocation_model)` is experimental and subject to change
--> $DIR/feature-gate-cfg-relocation-model.rs:1:7
|
LL | #[cfg(relocation_model = "pic")]
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #114929 <https://github.com/rust-lang/rust/issues/114929> for more information
= help: add `#![feature(cfg_relocation_model)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.