Skip to content

Commit

Permalink
Refactor determining of relocation model into methods
Browse files Browse the repository at this point in the history
  • Loading branch information
badboy committed Jul 29, 2016
1 parent 1bc0447 commit 1798c1a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
26 changes: 3 additions & 23 deletions src/librustc_trans/back/write.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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,
)
Expand Down
39 changes: 38 additions & 1 deletion src/librustc_trans/context.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 1798c1a

Please sign in to comment.