about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2020-04-23 00:46:45 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2020-04-26 11:18:47 +0300
commitfb91e5ed2fe72c6ce38abe0ec2ca47cbeac78d8d (patch)
treec1079e34d3118a1cf9ee561296a1814253327fe1
parent019ab732ce63a117cbb446db1488916c5c0bd2a7 (diff)
downloadrust-fb91e5ed2fe72c6ce38abe0ec2ca47cbeac78d8d.tar.gz
rust-fb91e5ed2fe72c6ce38abe0ec2ca47cbeac78d8d.zip
rustc_target: Stop using "string typing" for relocation models
Introduce `enum RelocModel` instead.
-rw-r--r--src/librustc_codegen_llvm/back/write.rs26
-rw-r--r--src/librustc_codegen_llvm/context.rs27
-rw-r--r--src/librustc_codegen_llvm/lib.rs4
-rw-r--r--src/librustc_codegen_llvm/llvm/ffi.rs1
-rw-r--r--src/librustc_codegen_ssa/back/link.rs13
-rw-r--r--src/librustc_interface/tests.rs4
-rw-r--r--src/librustc_session/config.rs7
-rw-r--r--src/librustc_session/options.rs20
-rw-r--r--src/librustc_session/session.rs6
-rw-r--r--src/librustc_target/spec/aarch64_unknown_none.rs4
-rw-r--r--src/librustc_target/spec/aarch64_unknown_none_softfloat.rs4
-rw-r--r--src/librustc_target/spec/armebv7r_none_eabi.rs5
-rw-r--r--src/librustc_target/spec/armebv7r_none_eabihf.rs5
-rw-r--r--src/librustc_target/spec/armv7a_none_eabi.rs4
-rw-r--r--src/librustc_target/spec/armv7a_none_eabihf.rs4
-rw-r--r--src/librustc_target/spec/armv7r_none_eabi.rs5
-rw-r--r--src/librustc_target/spec/armv7r_none_eabihf.rs5
-rw-r--r--src/librustc_target/spec/hermit_base.rs4
-rw-r--r--src/librustc_target/spec/hermit_kernel_base.rs4
-rw-r--r--src/librustc_target/spec/linux_kernel_base.rs4
-rw-r--r--src/librustc_target/spec/mod.rs60
-rw-r--r--src/librustc_target/spec/msp430_none_elf.rs4
-rw-r--r--src/librustc_target/spec/riscv32i_unknown_none_elf.rs5
-rw-r--r--src/librustc_target/spec/riscv32imac_unknown_none_elf.rs5
-rw-r--r--src/librustc_target/spec/riscv32imc_unknown_none_elf.rs5
-rw-r--r--src/librustc_target/spec/riscv64gc_unknown_none_elf.rs5
-rw-r--r--src/librustc_target/spec/riscv64imac_unknown_none_elf.rs5
-rw-r--r--src/librustc_target/spec/thumb_base.rs4
-rw-r--r--src/librustc_target/spec/wasm32_base.rs4
-rw-r--r--src/rustllvm/PassWrapper.cpp5
30 files changed, 150 insertions, 108 deletions
diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs
index b57ad102d63..8630d417359 100644
--- a/src/librustc_codegen_llvm/back/write.rs
+++ b/src/librustc_codegen_llvm/back/write.rs
@@ -7,7 +7,7 @@ use crate::back::profiling::{
 use crate::base;
 use crate::common;
 use crate::consts;
-use crate::context::{get_reloc_model, is_pie_binary};
+use crate::context::is_pie_binary;
 use crate::llvm::{self, DiagnosticInfo, PassManager, SMDiagnostic};
 use crate::llvm_util;
 use crate::type_::Type;
@@ -25,6 +25,7 @@ use rustc_middle::bug;
 use rustc_middle::ty::TyCtxt;
 use rustc_session::config::{self, Lto, OutputType, Passes, Sanitizer, SwitchWithOptPath};
 use rustc_session::Session;
+use rustc_target::spec::RelocModel;
 
 use libc::{c_char, c_int, c_uint, c_void, size_t};
 use std::ffi::CString;
@@ -35,16 +36,6 @@ use std::slice;
 use std::str;
 use std::sync::Arc;
 
-pub const RELOC_MODEL_ARGS: [(&str, llvm::RelocMode); 7] = [
-    ("pic", llvm::RelocMode::PIC),
-    ("static", llvm::RelocMode::Static),
-    ("default", llvm::RelocMode::Default),
-    ("dynamic-no-pic", llvm::RelocMode::DynamicNoPic),
-    ("ropi", llvm::RelocMode::ROPI),
-    ("rwpi", llvm::RelocMode::RWPI),
-    ("ropi-rwpi", llvm::RelocMode::ROPI_RWPI),
-];
-
 pub const CODE_GEN_MODEL_ARGS: &[(&str, llvm::CodeModel)] = &[
     ("small", llvm::CodeModel::Small),
     ("kernel", llvm::CodeModel::Kernel),
@@ -126,6 +117,17 @@ fn to_pass_builder_opt_level(cfg: config::OptLevel) -> llvm::PassBuilderOptLevel
     }
 }
 
+fn to_llvm_relocation_model(relocation_model: RelocModel) -> llvm::RelocMode {
+    match relocation_model {
+        RelocModel::Static => llvm::RelocMode::Static,
+        RelocModel::Pic => llvm::RelocMode::PIC,
+        RelocModel::DynamicNoPic => llvm::RelocMode::DynamicNoPic,
+        RelocModel::Ropi => llvm::RelocMode::ROPI,
+        RelocModel::Rwpi => llvm::RelocMode::RWPI,
+        RelocModel::RopiRwpi => llvm::RelocMode::ROPI_RWPI,
+    }
+}
+
 // If find_features is true this won't access `sess.crate_types` by assuming
 // that `is_pie_binary` is false. When we discover LLVM target features
 // `sess.crate_types` is uninitialized so we cannot access it.
@@ -134,7 +136,7 @@ pub fn target_machine_factory(
     optlvl: config::OptLevel,
     find_features: bool,
 ) -> Arc<dyn Fn() -> Result<&'static mut llvm::TargetMachine, String> + Send + Sync> {
-    let reloc_model = get_reloc_model(sess);
+    let reloc_model = to_llvm_relocation_model(sess.relocation_model());
 
     let (opt_level, _) = to_llvm_opt_settings(optlvl);
     let use_softfp = sess.opts.cg.soft_float;
diff --git a/src/librustc_codegen_llvm/context.rs b/src/librustc_codegen_llvm/context.rs
index d385c073074..f614df6045c 100644
--- a/src/librustc_codegen_llvm/context.rs
+++ b/src/librustc_codegen_llvm/context.rs
@@ -21,7 +21,7 @@ use rustc_session::Session;
 use rustc_span::source_map::{Span, DUMMY_SP};
 use rustc_span::symbol::Symbol;
 use rustc_target::abi::{HasDataLayout, LayoutOf, PointeeInfo, Size, TargetDataLayout, VariantIdx};
-use rustc_target::spec::{HasTargetSpec, Target};
+use rustc_target::spec::{HasTargetSpec, RelocModel, Target};
 
 use std::cell::{Cell, RefCell};
 use std::ffi::CStr;
@@ -87,22 +87,6 @@ pub struct CodegenCx<'ll, 'tcx> {
     local_gen_sym_counter: Cell<usize>,
 }
 
-pub fn get_reloc_model(sess: &Session) -> llvm::RelocMode {
-    let reloc_model_arg = match sess.opts.cg.relocation_model {
-        Some(ref s) => &s[..],
-        None => &sess.target.target.options.relocation_model[..],
-    };
-
-    match crate::back::write::RELOC_MODEL_ARGS.iter().find(|&&arg| arg.0 == reloc_model_arg) {
-        Some(x) => x.1,
-        _ => {
-            sess.err(&format!("{:?} is not a valid relocation mode", reloc_model_arg));
-            sess.abort_if_errors();
-            bug!();
-        }
-    }
-}
-
 fn get_tls_model(sess: &Session) -> llvm::ThreadLocalMode {
     let tls_model_arg = match sess.opts.debugging_opts.tls_model {
         Some(ref s) => &s[..],
@@ -119,12 +103,9 @@ fn get_tls_model(sess: &Session) -> llvm::ThreadLocalMode {
     }
 }
 
-fn is_any_library(sess: &Session) -> bool {
-    sess.crate_types.borrow().iter().any(|ty| *ty != config::CrateType::Executable)
-}
-
 pub fn is_pie_binary(sess: &Session) -> bool {
-    !is_any_library(sess) && get_reloc_model(sess) == llvm::RelocMode::PIC
+    sess.relocation_model() == RelocModel::Pic
+        && !sess.crate_types.borrow().iter().any(|ty| *ty != config::CrateType::Executable)
 }
 
 fn strip_function_ptr_alignment(data_layout: String) -> String {
@@ -200,7 +181,7 @@ pub unsafe fn create_module(
     let llvm_target = SmallCStr::new(&sess.target.target.llvm_target);
     llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
 
-    if get_reloc_model(sess) == llvm::RelocMode::PIC {
+    if sess.relocation_model() == RelocModel::Pic {
         llvm::LLVMRustSetModulePICLevel(llmod);
     }
 
diff --git a/src/librustc_codegen_llvm/lib.rs b/src/librustc_codegen_llvm/lib.rs
index 64158679740..9d6f2150841 100644
--- a/src/librustc_codegen_llvm/lib.rs
+++ b/src/librustc_codegen_llvm/lib.rs
@@ -201,7 +201,9 @@ impl CodegenBackend for LlvmCodegenBackend {
         match req {
             PrintRequest::RelocationModels => {
                 println!("Available relocation models:");
-                for &(name, _) in back::write::RELOC_MODEL_ARGS.iter() {
+                for name in
+                    &["static", "pic", "dynamic-no-pic", "ropi", "rwpi", "ropi-rwpi", "default"]
+                {
                     println!("    {}", name);
                 }
                 println!();
diff --git a/src/librustc_codegen_llvm/llvm/ffi.rs b/src/librustc_codegen_llvm/llvm/ffi.rs
index aeb34e5c9c9..85e90226f98 100644
--- a/src/librustc_codegen_llvm/llvm/ffi.rs
+++ b/src/librustc_codegen_llvm/llvm/ffi.rs
@@ -446,7 +446,6 @@ pub struct SanitizerOptions {
 #[derive(Copy, Clone, PartialEq)]
 #[repr(C)]
 pub enum RelocMode {
-    Default,
     Static,
     PIC,
     DynamicNoPic,
diff --git a/src/librustc_codegen_ssa/back/link.rs b/src/librustc_codegen_ssa/back/link.rs
index 8725bfaa025..9d42db8ec42 100644
--- a/src/librustc_codegen_ssa/back/link.rs
+++ b/src/librustc_codegen_ssa/back/link.rs
@@ -12,7 +12,7 @@ use rustc_session::search_paths::PathKind;
 /// need out of the shared crate context before we get rid of it.
 use rustc_session::{filesearch, Session};
 use rustc_span::symbol::Symbol;
-use rustc_target::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelroLevel};
+use rustc_target::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, RelroLevel};
 
 use super::archive::ArchiveBuilder;
 use super::command::Command;
@@ -1352,7 +1352,7 @@ fn add_position_independent_executable_args(
     if sess.target.target.options.position_independent_executables {
         let attr_link_args = &*codegen_results.crate_info.link_args;
         let mut user_defined_link_args = sess.opts.cg.link_args.iter().chain(attr_link_args);
-        if is_pic(sess)
+        if sess.relocation_model() == RelocModel::Pic
             && !sess.crt_static(Some(crate_type))
             && !user_defined_link_args.any(|x| x == "-static")
         {
@@ -1992,12 +1992,3 @@ fn are_upstream_rust_objects_already_included(sess: &Session) -> bool {
         config::Lto::No | config::Lto::ThinLocal => false,
     }
 }
-
-fn is_pic(sess: &Session) -> bool {
-    let reloc_model_arg = match sess.opts.cg.relocation_model {
-        Some(ref s) => &s[..],
-        None => &sess.target.target.options.relocation_model[..],
-    };
-
-    reloc_model_arg == "pic"
-}
diff --git a/src/librustc_interface/tests.rs b/src/librustc_interface/tests.rs
index 211850486c0..f0e7581b760 100644
--- a/src/librustc_interface/tests.rs
+++ b/src/librustc_interface/tests.rs
@@ -14,7 +14,7 @@ use rustc_session::{build_session, Session};
 use rustc_span::edition::{Edition, DEFAULT_EDITION};
 use rustc_span::symbol::sym;
 use rustc_span::SourceFileHashAlgorithm;
-use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
+use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelocModel, RelroLevel};
 use std::collections::{BTreeMap, BTreeSet};
 use std::iter::FromIterator;
 use std::path::PathBuf;
@@ -430,7 +430,7 @@ fn test_codegen_options_tracking_hash() {
     tracked!(prefer_dynamic, true);
     tracked!(profile_generate, SwitchWithOptPath::Enabled(None));
     tracked!(profile_use, Some(PathBuf::from("abc")));
-    tracked!(relocation_model, Some(String::from("relocation model")));
+    tracked!(relocation_model, Some(RelocModel::Pic));
     tracked!(soft_float, true);
     tracked!(target_cpu, Some(String::from("abc")));
     tracked!(target_feature, String::from("all the features, all of them"));
diff --git a/src/librustc_session/config.rs b/src/librustc_session/config.rs
index f6d7e091e00..1ab02f84c11 100644
--- a/src/librustc_session/config.rs
+++ b/src/librustc_session/config.rs
@@ -1311,10 +1311,6 @@ fn collect_print_requests(
         prints.push(PrintRequest::TargetFeatures);
         cg.target_feature = String::new();
     }
-    if cg.relocation_model.as_ref().map_or(false, |s| s == "help") {
-        prints.push(PrintRequest::RelocationModels);
-        cg.relocation_model = None;
-    }
     if cg.code_model.as_ref().map_or(false, |s| s == "help") {
         prints.push(PrintRequest::CodeModels);
         cg.code_model = None;
@@ -2005,7 +2001,7 @@ crate mod dep_tracking {
     use crate::utils::NativeLibraryKind;
     use rustc_feature::UnstableFeatures;
     use rustc_span::edition::Edition;
-    use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel, TargetTriple};
+    use rustc_target::spec::{MergeFunctions, PanicStrategy, RelocModel, RelroLevel, TargetTriple};
     use std::collections::hash_map::DefaultHasher;
     use std::collections::BTreeMap;
     use std::hash::Hash;
@@ -2053,6 +2049,7 @@ crate mod dep_tracking {
     impl_dep_tracking_hash_via_hash!(Option<(String, u64)>);
     impl_dep_tracking_hash_via_hash!(Option<Vec<String>>);
     impl_dep_tracking_hash_via_hash!(Option<MergeFunctions>);
+    impl_dep_tracking_hash_via_hash!(Option<RelocModel>);
     impl_dep_tracking_hash_via_hash!(Option<PanicStrategy>);
     impl_dep_tracking_hash_via_hash!(Option<RelroLevel>);
     impl_dep_tracking_hash_via_hash!(Option<lint::Level>);
diff --git a/src/librustc_session/options.rs b/src/librustc_session/options.rs
index 54b2f2fe470..804e929bc81 100644
--- a/src/librustc_session/options.rs
+++ b/src/librustc_session/options.rs
@@ -6,7 +6,7 @@ use crate::search_paths::SearchPath;
 use crate::utils::NativeLibraryKind;
 
 use rustc_target::spec::TargetTriple;
-use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
+use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelocModel, RelroLevel};
 
 use rustc_feature::UnstableFeatures;
 use rustc_span::edition::Edition;
@@ -265,14 +265,13 @@ macro_rules! options {
         pub const parse_merge_functions: &str = "one of: `disabled`, `trampolines`, or `aliases`";
         pub const parse_symbol_mangling_version: &str = "either `legacy` or `v0` (RFC 2603)";
         pub const parse_src_file_hash: &str = "either `md5` or `sha1`";
+        pub const parse_relocation_model: &str =
+            "one of supported relocation models (`rustc --print relocation-models`)";
     }
 
     #[allow(dead_code)]
     mod $mod_set {
-        use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath,
-            SymbolManglingVersion, CFGuard, SourceFileHashAlgorithm};
-        use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
-        use std::path::PathBuf;
+        use super::*;
         use std::str::FromStr;
 
         // Sometimes different options need to build a common structure.
@@ -598,6 +597,15 @@ macro_rules! options {
             true
         }
 
+        fn parse_relocation_model(slot: &mut Option<RelocModel>, v: Option<&str>) -> bool {
+            match v.and_then(|s| RelocModel::from_str(s).ok()) {
+                Some(relocation_model) => *slot = Some(relocation_model),
+                None if v == Some("default") => *slot = None,
+                _ => return false,
+            }
+            true
+        }
+
         fn parse_symbol_mangling_version(
             slot: &mut SymbolManglingVersion,
             v: Option<&str>,
@@ -697,7 +705,7 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
         "compile the program with profiling instrumentation"),
     profile_use: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
         "use the given `.profdata` file for profile-guided optimization"),
-    relocation_model: Option<String> = (None, parse_opt_string, [TRACKED],
+    relocation_model: Option<RelocModel> = (None, parse_relocation_model, [TRACKED],
         "choose the relocation model to use (`rustc --print relocation-models` for details)"),
     remark: Passes = (Passes::Some(Vec::new()), parse_passes, [UNTRACKED],
         "print remarks for these optimization passes (space separated, or \"all\")"),
diff --git a/src/librustc_session/session.rs b/src/librustc_session/session.rs
index f0bbe1d0a18..226d9392095 100644
--- a/src/librustc_session/session.rs
+++ b/src/librustc_session/session.rs
@@ -22,7 +22,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId, ErrorReported
 use rustc_span::edition::Edition;
 use rustc_span::source_map::{self, FileLoader, MultiSpan, RealFileLoader, SourceMap, Span};
 use rustc_span::SourceFileHashAlgorithm;
-use rustc_target::spec::{PanicStrategy, RelroLevel, Target, TargetTriple};
+use rustc_target::spec::{PanicStrategy, RelocModel, RelroLevel, Target, TargetTriple};
 
 use std::cell::{self, RefCell};
 use std::env;
@@ -584,6 +584,10 @@ impl Session {
         }
     }
 
+    pub fn relocation_model(&self) -> RelocModel {
+        self.opts.cg.relocation_model.unwrap_or(self.target.target.options.relocation_model)
+    }
+
     pub fn must_not_eliminate_frame_pointers(&self) -> bool {
         // "mcount" function relies on stack pointer.
         // See <https://sourceware.org/binutils/docs/gprof/Implementation.html>.
diff --git a/src/librustc_target/spec/aarch64_unknown_none.rs b/src/librustc_target/spec/aarch64_unknown_none.rs
index 13e62b60ca8..7177c4e251e 100644
--- a/src/librustc_target/spec/aarch64_unknown_none.rs
+++ b/src/librustc_target/spec/aarch64_unknown_none.rs
@@ -6,14 +6,14 @@
 //
 // For example, `-C target-cpu=cortex-a53`.
 
-use super::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions};
+use super::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, Target, TargetOptions};
 
 pub fn target() -> Result<Target, String> {
     let opts = TargetOptions {
         linker: Some("rust-lld".to_owned()),
         features: "+strict-align,+neon,+fp-armv8".to_string(),
         executables: true,
-        relocation_model: "static".to_string(),
+        relocation_model: RelocModel::Static,
         disable_redzone: true,
         linker_is_gnu: true,
         max_atomic_width: Some(128),
diff --git a/src/librustc_target/spec/aarch64_unknown_none_softfloat.rs b/src/librustc_target/spec/aarch64_unknown_none_softfloat.rs
index cf46252555c..986300c677d 100644
--- a/src/librustc_target/spec/aarch64_unknown_none_softfloat.rs
+++ b/src/librustc_target/spec/aarch64_unknown_none_softfloat.rs
@@ -6,14 +6,14 @@
 //
 // For example, `-C target-cpu=cortex-a53`.
 
-use super::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions};
+use super::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, Target, TargetOptions};
 
 pub fn target() -> Result<Target, String> {
     let opts = TargetOptions {
         linker: Some("rust-lld".to_owned()),
         features: "+strict-align,-neon,-fp-armv8".to_string(),
         executables: true,
-        relocation_model: "static".to_string(),
+        relocation_model: RelocModel::Static,
         disable_redzone: true,
         linker_is_gnu: true,
         max_atomic_width: Some(128),
diff --git a/src/librustc_target/spec/armebv7r_none_eabi.rs b/src/librustc_target/spec/armebv7r_none_eabi.rs
index ebe901e4f27..a1f68f6706a 100644
--- a/src/librustc_target/spec/armebv7r_none_eabi.rs
+++ b/src/librustc_target/spec/armebv7r_none_eabi.rs
@@ -1,6 +1,7 @@
 // Targets the Big endian Cortex-R4/R5 processor (ARMv7-R)
 
-use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions, TargetResult};
+use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel};
+use crate::spec::{Target, TargetOptions, TargetResult};
 
 pub fn target() -> TargetResult {
     Ok(Target {
@@ -18,7 +19,7 @@ pub fn target() -> TargetResult {
         options: TargetOptions {
             executables: true,
             linker: Some("rust-lld".to_owned()),
-            relocation_model: "static".to_string(),
+            relocation_model: RelocModel::Static,
             panic_strategy: PanicStrategy::Abort,
             max_atomic_width: Some(32),
             abi_blacklist: super::arm_base::abi_blacklist(),
diff --git a/src/librustc_target/spec/armebv7r_none_eabihf.rs b/src/librustc_target/spec/armebv7r_none_eabihf.rs
index 8652d1051ad..4d81c21f52a 100644
--- a/src/librustc_target/spec/armebv7r_none_eabihf.rs
+++ b/src/librustc_target/spec/armebv7r_none_eabihf.rs
@@ -1,6 +1,7 @@
 // Targets the Cortex-R4F/R5F processor (ARMv7-R)
 
-use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions, TargetResult};
+use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel};
+use crate::spec::{Target, TargetOptions, TargetResult};
 
 pub fn target() -> TargetResult {
     Ok(Target {
@@ -18,7 +19,7 @@ pub fn target() -> TargetResult {
         options: TargetOptions {
             executables: true,
             linker: Some("rust-lld".to_owned()),
-            relocation_model: "static".to_string(),
+            relocation_model: RelocModel::Static,
             panic_strategy: PanicStrategy::Abort,
             features: "+vfp3,-d32,-fp16".to_string(),
             max_atomic_width: Some(32),
diff --git a/src/librustc_target/spec/armv7a_none_eabi.rs b/src/librustc_target/spec/armv7a_none_eabi.rs
index 2fbef154f81..09f1494e81c 100644
--- a/src/librustc_target/spec/armv7a_none_eabi.rs
+++ b/src/librustc_target/spec/armv7a_none_eabi.rs
@@ -17,14 +17,14 @@
 // - `relocation-model` set to `static`; also no PIE, no relro and no dynamic
 // linking. rationale: matches `thumb` targets
 
-use super::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions};
+use super::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, Target, TargetOptions};
 
 pub fn target() -> Result<Target, String> {
     let opts = TargetOptions {
         linker: Some("rust-lld".to_owned()),
         features: "+v7,+thumb2,+soft-float,-neon,+strict-align".to_string(),
         executables: true,
-        relocation_model: "static".to_string(),
+        relocation_model: RelocModel::Static,
         disable_redzone: true,
         max_atomic_width: Some(64),
         panic_strategy: PanicStrategy::Abort,
diff --git a/src/librustc_target/spec/armv7a_none_eabihf.rs b/src/librustc_target/spec/armv7a_none_eabihf.rs
index f31e68c5bd1..653ca76435b 100644
--- a/src/librustc_target/spec/armv7a_none_eabihf.rs
+++ b/src/librustc_target/spec/armv7a_none_eabihf.rs
@@ -5,14 +5,14 @@
 // changes (list in `armv7a_none_eabi.rs`) to bring it closer to the bare-metal
 // `thumb` & `aarch64` targets.
 
-use super::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions};
+use super::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, Target, TargetOptions};
 
 pub fn target() -> Result<Target, String> {
     let opts = TargetOptions {
         linker: Some("rust-lld".to_owned()),
         features: "+v7,+vfp3,-d32,+thumb2,-neon,+strict-align".to_string(),
         executables: true,
-        relocation_model: "static".to_string(),
+        relocation_model: RelocModel::Static,
         disable_redzone: true,
         max_atomic_width: Some(64),
         panic_strategy: PanicStrategy::Abort,
diff --git a/src/librustc_target/spec/armv7r_none_eabi.rs b/src/librustc_target/spec/armv7r_none_eabi.rs
index b7fcda63db0..29dfa170397 100644
--- a/src/librustc_target/spec/armv7r_none_eabi.rs
+++ b/src/librustc_target/spec/armv7r_none_eabi.rs
@@ -1,6 +1,7 @@
 // Targets the Little-endian Cortex-R4/R5 processor (ARMv7-R)
 
-use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions, TargetResult};
+use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel};
+use crate::spec::{Target, TargetOptions, TargetResult};
 
 pub fn target() -> TargetResult {
     Ok(Target {
@@ -18,7 +19,7 @@ pub fn target() -> TargetResult {
         options: TargetOptions {
             executables: true,
             linker: Some("rust-lld".to_owned()),
-            relocation_model: "static".to_string(),
+            relocation_model: RelocModel::Static,
             panic_strategy: PanicStrategy::Abort,
             max_atomic_width: Some(32),
             abi_blacklist: super::arm_base::abi_blacklist(),
diff --git a/src/librustc_target/spec/armv7r_none_eabihf.rs b/src/librustc_target/spec/armv7r_none_eabihf.rs
index 340090fd43b..e6b0187c331 100644
--- a/src/librustc_target/spec/armv7r_none_eabihf.rs
+++ b/src/librustc_target/spec/armv7r_none_eabihf.rs
@@ -1,6 +1,7 @@
 // Targets the Little-endian Cortex-R4F/R5F processor (ARMv7-R)
 
-use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions, TargetResult};
+use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel};
+use crate::spec::{Target, TargetOptions, TargetResult};
 
 pub fn target() -> TargetResult {
     Ok(Target {
@@ -18,7 +19,7 @@ pub fn target() -> TargetResult {
         options: TargetOptions {
             executables: true,
             linker: Some("rust-lld".to_owned()),
-            relocation_model: "static".to_string(),
+            relocation_model: RelocModel::Static,
             panic_strategy: PanicStrategy::Abort,
             features: "+vfp3,-d32,-fp16".to_string(),
             max_atomic_width: Some(32),
diff --git a/src/librustc_target/spec/hermit_base.rs b/src/librustc_target/spec/hermit_base.rs
index b9f94023e7a..cb12055290e 100644
--- a/src/librustc_target/spec/hermit_base.rs
+++ b/src/librustc_target/spec/hermit_base.rs
@@ -1,4 +1,4 @@
-use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, PanicStrategy, TargetOptions};
+use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, TargetOptions};
 
 pub fn opts() -> TargetOptions {
     let mut pre_link_args = LinkArgs::new();
@@ -15,7 +15,7 @@ pub fn opts() -> TargetOptions {
         pre_link_args,
         panic_strategy: PanicStrategy::Abort,
         position_independent_executables: true,
-        relocation_model: "static".to_string(),
+        relocation_model: RelocModel::Static,
         target_family: None,
         tls_model: "initial-exec".to_string(),
         ..Default::default()
diff --git a/src/librustc_target/spec/hermit_kernel_base.rs b/src/librustc_target/spec/hermit_kernel_base.rs
index 1f9b195e2e6..11599fda409 100644
--- a/src/librustc_target/spec/hermit_kernel_base.rs
+++ b/src/librustc_target/spec/hermit_kernel_base.rs
@@ -1,4 +1,4 @@
-use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, PanicStrategy, TargetOptions};
+use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, TargetOptions};
 
 pub fn opts() -> TargetOptions {
     let mut pre_link_args = LinkArgs::new();
@@ -16,7 +16,7 @@ pub fn opts() -> TargetOptions {
         pre_link_args,
         panic_strategy: PanicStrategy::Abort,
         position_independent_executables: true,
-        relocation_model: "static".to_string(),
+        relocation_model: RelocModel::Static,
         target_family: None,
         tls_model: "initial-exec".to_string(),
         ..Default::default()
diff --git a/src/librustc_target/spec/linux_kernel_base.rs b/src/librustc_target/spec/linux_kernel_base.rs
index 4a900d1b02c..201d6a0fff9 100644
--- a/src/librustc_target/spec/linux_kernel_base.rs
+++ b/src/librustc_target/spec/linux_kernel_base.rs
@@ -1,4 +1,4 @@
-use crate::spec::{LinkArgs, LinkerFlavor, PanicStrategy, RelroLevel, TargetOptions};
+use crate::spec::{LinkArgs, LinkerFlavor, PanicStrategy, RelocModel, RelroLevel, TargetOptions};
 
 pub fn opts() -> TargetOptions {
     let mut pre_link_args = LinkArgs::new();
@@ -16,7 +16,7 @@ pub fn opts() -> TargetOptions {
         position_independent_executables: true,
         needs_plt: true,
         relro_level: RelroLevel::Full,
-        relocation_model: "static".to_string(),
+        relocation_model: RelocModel::Static,
         target_family: Some("unix".to_string()),
         pre_link_args,
 
diff --git a/src/librustc_target/spec/mod.rs b/src/librustc_target/spec/mod.rs
index d9040463317..77fc78e8148 100644
--- a/src/librustc_target/spec/mod.rs
+++ b/src/librustc_target/spec/mod.rs
@@ -265,6 +265,46 @@ impl ToJson for MergeFunctions {
     }
 }
 
+#[derive(Clone, Copy, PartialEq, Hash, Debug)]
+pub enum RelocModel {
+    Static,
+    Pic,
+    DynamicNoPic,
+    Ropi,
+    Rwpi,
+    RopiRwpi,
+}
+
+impl FromStr for RelocModel {
+    type Err = ();
+
+    fn from_str(s: &str) -> Result<RelocModel, ()> {
+        Ok(match s {
+            "static" => RelocModel::Static,
+            "pic" => RelocModel::Pic,
+            "dynamic-no-pic" => RelocModel::DynamicNoPic,
+            "ropi" => RelocModel::Ropi,
+            "rwpi" => RelocModel::Rwpi,
+            "ropi-rwpi" => RelocModel::RopiRwpi,
+            _ => return Err(()),
+        })
+    }
+}
+
+impl ToJson for RelocModel {
+    fn to_json(&self) -> Json {
+        match *self {
+            RelocModel::Static => "static",
+            RelocModel::Pic => "pic",
+            RelocModel::DynamicNoPic => "dynamic-no-pic",
+            RelocModel::Ropi => "ropi",
+            RelocModel::Rwpi => "rwpi",
+            RelocModel::RopiRwpi => "ropi-rwpi",
+        }
+        .to_json()
+    }
+}
+
 pub enum LoadTargetError {
     BuiltinTargetNotFound(String),
     Other(String),
@@ -614,8 +654,8 @@ pub struct TargetOptions {
     /// libraries. Defaults to false.
     pub executables: bool,
     /// Relocation model to use in object file. Corresponds to `llc
-    /// -relocation-model=$relocation_model`. Defaults to "pic".
-    pub relocation_model: String,
+    /// -relocation-model=$relocation_model`. Defaults to `Pic`.
+    pub relocation_model: RelocModel,
     /// Code model to use. Corresponds to `llc -code-model=$code_model`.
     pub code_model: Option<String>,
     /// TLS model to use. Options are "global-dynamic" (default), "local-dynamic", "initial-exec"
@@ -821,7 +861,7 @@ impl Default for TargetOptions {
             dynamic_linking: false,
             only_cdylib: false,
             executables: false,
-            relocation_model: "pic".to_string(),
+            relocation_model: RelocModel::Pic,
             code_model: None,
             tls_model: "global-dynamic".to_string(),
             disable_redzone: false,
@@ -1008,6 +1048,18 @@ impl Target {
                     Some(Ok(()))
                 })).unwrap_or(Ok(()))
             } );
+            ($key_name:ident, RelocModel) => ( {
+                let name = (stringify!($key_name)).replace("_", "-");
+                obj.find(&name[..]).and_then(|o| o.as_string().and_then(|s| {
+                    match s.parse::<RelocModel>() {
+                        Ok(relocation_model) => base.options.$key_name = relocation_model,
+                        _ => return Some(Err(format!("'{}' is not a valid relocation model. \
+                                                      Run `rustc --print relocation-models` to \
+                                                      see the list of supported values.", s))),
+                    }
+                    Some(Ok(()))
+                })).unwrap_or(Ok(()))
+            } );
             ($key_name:ident, PanicStrategy) => ( {
                 let name = (stringify!($key_name)).replace("_", "-");
                 obj.find(&name[..]).and_then(|o| o.as_string().and_then(|s| {
@@ -1146,7 +1198,7 @@ impl Target {
         key!(dynamic_linking, bool);
         key!(only_cdylib, bool);
         key!(executables, bool);
-        key!(relocation_model);
+        key!(relocation_model, RelocModel)?;
         key!(code_model, optional);
         key!(tls_model);
         key!(disable_redzone, bool);
diff --git a/src/librustc_target/spec/msp430_none_elf.rs b/src/librustc_target/spec/msp430_none_elf.rs
index 9a90ac7ccec..c6d0308f8f8 100644
--- a/src/librustc_target/spec/msp430_none_elf.rs
+++ b/src/librustc_target/spec/msp430_none_elf.rs
@@ -1,4 +1,4 @@
-use crate::spec::{LinkerFlavor, PanicStrategy, Target, TargetOptions, TargetResult};
+use crate::spec::{LinkerFlavor, PanicStrategy, RelocModel, Target, TargetOptions, TargetResult};
 
 pub fn target() -> TargetResult {
     Ok(Target {
@@ -40,7 +40,7 @@ pub fn target() -> TargetResult {
 
             // Similarly, one almost always never wants to use relocatable
             // code because of the extra costs it involves.
-            relocation_model: "static".to_string(),
+            relocation_model: RelocModel::Static,
 
             // Right now we invoke an external assembler and this isn't
             // compatible with multiple codegen units, and plus we probably
diff --git a/src/librustc_target/spec/riscv32i_unknown_none_elf.rs b/src/librustc_target/spec/riscv32i_unknown_none_elf.rs
index a7020bc8f91..aade1e70823 100644
--- a/src/librustc_target/spec/riscv32i_unknown_none_elf.rs
+++ b/src/librustc_target/spec/riscv32i_unknown_none_elf.rs
@@ -1,4 +1,5 @@
-use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions, TargetResult};
+use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel};
+use crate::spec::{Target, TargetOptions, TargetResult};
 
 pub fn target() -> TargetResult {
     Ok(Target {
@@ -21,7 +22,7 @@ pub fn target() -> TargetResult {
             features: String::new(),
             executables: true,
             panic_strategy: PanicStrategy::Abort,
-            relocation_model: "static".to_string(),
+            relocation_model: RelocModel::Static,
             emit_debug_gdb_scripts: false,
             abi_blacklist: super::riscv_base::abi_blacklist(),
             eliminate_frame_pointer: false,
diff --git a/src/librustc_target/spec/riscv32imac_unknown_none_elf.rs b/src/librustc_target/spec/riscv32imac_unknown_none_elf.rs
index 6ec49410aeb..e2990eeb826 100644
--- a/src/librustc_target/spec/riscv32imac_unknown_none_elf.rs
+++ b/src/librustc_target/spec/riscv32imac_unknown_none_elf.rs
@@ -1,4 +1,5 @@
-use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions, TargetResult};
+use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel};
+use crate::spec::{Target, TargetOptions, TargetResult};
 
 pub fn target() -> TargetResult {
     Ok(Target {
@@ -21,7 +22,7 @@ pub fn target() -> TargetResult {
             features: "+m,+a,+c".to_string(),
             executables: true,
             panic_strategy: PanicStrategy::Abort,
-            relocation_model: "static".to_string(),
+            relocation_model: RelocModel::Static,
             emit_debug_gdb_scripts: false,
             abi_blacklist: super::riscv_base::abi_blacklist(),
             eliminate_frame_pointer: false,
diff --git a/src/librustc_target/spec/riscv32imc_unknown_none_elf.rs b/src/librustc_target/spec/riscv32imc_unknown_none_elf.rs
index 70346347fb3..55a4d58dfcc 100644
--- a/src/librustc_target/spec/riscv32imc_unknown_none_elf.rs
+++ b/src/librustc_target/spec/riscv32imc_unknown_none_elf.rs
@@ -1,4 +1,5 @@
-use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions, TargetResult};
+use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel};
+use crate::spec::{Target, TargetOptions, TargetResult};
 
 pub fn target() -> TargetResult {
     Ok(Target {
@@ -21,7 +22,7 @@ pub fn target() -> TargetResult {
             features: "+m,+c".to_string(),
             executables: true,
             panic_strategy: PanicStrategy::Abort,
-            relocation_model: "static".to_string(),
+            relocation_model: RelocModel::Static,
             emit_debug_gdb_scripts: false,
             abi_blacklist: super::riscv_base::abi_blacklist(),
             eliminate_frame_pointer: false,
diff --git a/src/librustc_target/spec/riscv64gc_unknown_none_elf.rs b/src/librustc_target/spec/riscv64gc_unknown_none_elf.rs
index ca0f3d64d4a..9dbbcb1f4bd 100644
--- a/src/librustc_target/spec/riscv64gc_unknown_none_elf.rs
+++ b/src/librustc_target/spec/riscv64gc_unknown_none_elf.rs
@@ -1,4 +1,5 @@
-use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions, TargetResult};
+use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel};
+use crate::spec::{Target, TargetOptions, TargetResult};
 
 pub fn target() -> TargetResult {
     Ok(Target {
@@ -21,7 +22,7 @@ pub fn target() -> TargetResult {
             features: "+m,+a,+f,+d,+c".to_string(),
             executables: true,
             panic_strategy: PanicStrategy::Abort,
-            relocation_model: "static".to_string(),
+            relocation_model: RelocModel::Static,
             code_model: Some("medium".to_string()),
             emit_debug_gdb_scripts: false,
             abi_blacklist: super::riscv_base::abi_blacklist(),
diff --git a/src/librustc_target/spec/riscv64imac_unknown_none_elf.rs b/src/librustc_target/spec/riscv64imac_unknown_none_elf.rs
index 614403fe02a..7e9390561a7 100644
--- a/src/librustc_target/spec/riscv64imac_unknown_none_elf.rs
+++ b/src/librustc_target/spec/riscv64imac_unknown_none_elf.rs
@@ -1,4 +1,5 @@
-use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions, TargetResult};
+use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel};
+use crate::spec::{Target, TargetOptions, TargetResult};
 
 pub fn target() -> TargetResult {
     Ok(Target {
@@ -21,7 +22,7 @@ pub fn target() -> TargetResult {
             features: "+m,+a,+c".to_string(),
             executables: true,
             panic_strategy: PanicStrategy::Abort,
-            relocation_model: "static".to_string(),
+            relocation_model: RelocModel::Static,
             code_model: Some("medium".to_string()),
             emit_debug_gdb_scripts: false,
             abi_blacklist: super::riscv_base::abi_blacklist(),
diff --git a/src/librustc_target/spec/thumb_base.rs b/src/librustc_target/spec/thumb_base.rs
index eca095b5942..646a149a336 100644
--- a/src/librustc_target/spec/thumb_base.rs
+++ b/src/librustc_target/spec/thumb_base.rs
@@ -27,7 +27,7 @@
 // differentiate these targets from our other `arm(v7)-*-*-gnueabi(hf)` targets in the context of
 // build scripts / gcc flags.
 
-use crate::spec::{PanicStrategy, TargetOptions};
+use crate::spec::{PanicStrategy, RelocModel, TargetOptions};
 
 pub fn opts() -> TargetOptions {
     // See rust-lang/rfcs#1645 for a discussion about these defaults
@@ -40,7 +40,7 @@ pub fn opts() -> TargetOptions {
         panic_strategy: PanicStrategy::Abort,
         // Similarly, one almost always never wants to use relocatable code because of the extra
         // costs it involves.
-        relocation_model: "static".to_string(),
+        relocation_model: RelocModel::Static,
         abi_blacklist: super::arm_base::abi_blacklist(),
         // When this section is added a volatile load to its start address is also generated. This
         // volatile load is a footgun as it can end up loading an invalid memory address, depending
diff --git a/src/librustc_target/spec/wasm32_base.rs b/src/librustc_target/spec/wasm32_base.rs
index 47e80e8db19..08bade2abf4 100644
--- a/src/librustc_target/spec/wasm32_base.rs
+++ b/src/librustc_target/spec/wasm32_base.rs
@@ -1,4 +1,4 @@
-use super::{LinkerFlavor, LldFlavor, PanicStrategy, TargetOptions};
+use super::{LinkerFlavor, LldFlavor, PanicStrategy, RelocModel, TargetOptions};
 use std::collections::BTreeMap;
 
 pub fn options() -> TargetOptions {
@@ -130,7 +130,7 @@ pub fn options() -> TargetOptions {
         // that eventually we can ship a `pic`-compatible standard library which
         // works with `static` as well (or works with some method of generating
         // non-relative calls and such later on).
-        relocation_model: "static".to_string(),
+        relocation_model: RelocModel::Static,
 
         // When the atomics feature is activated then these two keys matter,
         // otherwise they're basically ignored by the standard library. In this
diff --git a/src/rustllvm/PassWrapper.cpp b/src/rustllvm/PassWrapper.cpp
index b221c17b422..95c5eb2f183 100644
--- a/src/rustllvm/PassWrapper.cpp
+++ b/src/rustllvm/PassWrapper.cpp
@@ -347,7 +347,6 @@ static PassBuilder::OptimizationLevel fromRust(LLVMRustPassBuilderOptLevel Level
 }
 
 enum class LLVMRustRelocMode {
-  Default,
   Static,
   PIC,
   DynamicNoPic,
@@ -356,10 +355,8 @@ enum class LLVMRustRelocMode {
   ROPIRWPI,
 };
 
-static Optional<Reloc::Model> fromRust(LLVMRustRelocMode RustReloc) {
+static Reloc::Model fromRust(LLVMRustRelocMode RustReloc) {
   switch (RustReloc) {
-  case LLVMRustRelocMode::Default:
-    return None;
   case LLVMRustRelocMode::Static:
     return Reloc::Static;
   case LLVMRustRelocMode::PIC: