diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2020-04-23 00:46:45 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2020-04-26 11:18:47 +0300 |
| commit | fb91e5ed2fe72c6ce38abe0ec2ca47cbeac78d8d (patch) | |
| tree | c1079e34d3118a1cf9ee561296a1814253327fe1 /src/librustc_codegen_llvm | |
| parent | 019ab732ce63a117cbb446db1488916c5c0bd2a7 (diff) | |
| download | rust-fb91e5ed2fe72c6ce38abe0ec2ca47cbeac78d8d.tar.gz rust-fb91e5ed2fe72c6ce38abe0ec2ca47cbeac78d8d.zip | |
rustc_target: Stop using "string typing" for relocation models
Introduce `enum RelocModel` instead.
Diffstat (limited to 'src/librustc_codegen_llvm')
| -rw-r--r-- | src/librustc_codegen_llvm/back/write.rs | 26 | ||||
| -rw-r--r-- | src/librustc_codegen_llvm/context.rs | 27 | ||||
| -rw-r--r-- | src/librustc_codegen_llvm/lib.rs | 4 | ||||
| -rw-r--r-- | src/librustc_codegen_llvm/llvm/ffi.rs | 1 |
4 files changed, 21 insertions, 37 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, |
