Refactor determining of relocation model into methods
This commit is contained in:
parent
1bc0447260
commit
1798c1aa59
2 changed files with 41 additions and 24 deletions
|
|
@ -24,6 +24,7 @@ use util::fs::link_or_copy;
|
|||
use errors::{self, Handler, Level, DiagnosticBuilder};
|
||||
use errors::emitter::Emitter;
|
||||
use syntax_pos::MultiSpan;
|
||||
use context::{is_pie_binary, get_reloc_model};
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::{CStr, CString};
|
||||
|
|
@ -154,32 +155,11 @@ fn get_llvm_opt_size(optimize: config::OptLevel) -> llvm::CodeGenOptSize {
|
|||
}
|
||||
|
||||
pub fn create_target_machine(sess: &Session) -> TargetMachineRef {
|
||||
let reloc_model_arg = match sess.opts.cg.relocation_model {
|
||||
Some(ref s) => &s[..],
|
||||
None => &sess.target.target.options.relocation_model[..],
|
||||
};
|
||||
let reloc_model = match reloc_model_arg {
|
||||
"pic" => llvm::RelocPIC,
|
||||
"static" => llvm::RelocStatic,
|
||||
"default" => llvm::RelocDefault,
|
||||
"dynamic-no-pic" => llvm::RelocDynamicNoPic,
|
||||
_ => {
|
||||
sess.err(&format!("{:?} is not a valid relocation mode",
|
||||
sess.opts
|
||||
.cg
|
||||
.relocation_model));
|
||||
sess.abort_if_errors();
|
||||
bug!();
|
||||
}
|
||||
};
|
||||
let reloc_model = get_reloc_model(sess);
|
||||
|
||||
let opt_level = get_llvm_opt_level(sess.opts.optimize);
|
||||
let use_softfp = sess.opts.cg.soft_float;
|
||||
|
||||
let any_library = sess.crate_types.borrow().iter().any(|ty| {
|
||||
*ty != config::CrateTypeExecutable
|
||||
});
|
||||
|
||||
let ffunction_sections = sess.target.target.options.function_sections;
|
||||
let fdata_sections = ffunction_sections;
|
||||
|
||||
|
|
@ -220,7 +200,7 @@ pub fn create_target_machine(sess: &Session) -> TargetMachineRef {
|
|||
reloc_model,
|
||||
opt_level,
|
||||
use_softfp,
|
||||
!any_library && reloc_model == llvm::RelocPIC,
|
||||
is_pie_binary(sess),
|
||||
ffunction_sections,
|
||||
fdata_sections,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ use rustc::ty::subst::{Substs, VecPerParamSpace};
|
|||
use rustc::ty::{self, Ty, TyCtxt};
|
||||
use session::config::NoDebugInfo;
|
||||
use session::Session;
|
||||
use session::config;
|
||||
use symbol_map::SymbolMap;
|
||||
use util::sha2::Sha256;
|
||||
use util::nodemap::{NodeMap, NodeSet, DefIdMap, FnvHashMap, FnvHashSet};
|
||||
|
|
@ -322,6 +323,38 @@ impl<'a, 'tcx> Iterator for CrateContextMaybeIterator<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
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 reloc_model_arg {
|
||||
"pic" => llvm::RelocPIC,
|
||||
"static" => llvm::RelocStatic,
|
||||
"default" => llvm::RelocDefault,
|
||||
"dynamic-no-pic" => llvm::RelocDynamicNoPic,
|
||||
_ => {
|
||||
sess.err(&format!("{:?} is not a valid relocation mode",
|
||||
sess.opts
|
||||
.cg
|
||||
.relocation_model));
|
||||
sess.abort_if_errors();
|
||||
bug!();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_any_library(sess: &Session) -> bool {
|
||||
sess.crate_types.borrow().iter().any(|ty| {
|
||||
*ty != config::CrateTypeExecutable
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_pie_binary(sess: &Session) -> bool {
|
||||
!is_any_library(sess) && get_reloc_model(sess) == llvm::RelocPIC
|
||||
}
|
||||
|
||||
unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextRef, ModuleRef) {
|
||||
let llcx = llvm::LLVMContextCreate();
|
||||
let mod_name = CString::new(mod_name).unwrap();
|
||||
|
|
@ -352,7 +385,11 @@ unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextR
|
|||
let llvm_target = sess.target.target.llvm_target.as_bytes();
|
||||
let llvm_target = CString::new(llvm_target).unwrap();
|
||||
llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
|
||||
llvm::LLVMRustSetModulePIELevel(llmod);
|
||||
|
||||
if is_pie_binary(sess) {
|
||||
llvm::LLVMRustSetModulePIELevel(llmod);
|
||||
}
|
||||
|
||||
(llcx, llmod)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue