Skip to content

Commit 1798c1a

Browse files
committed
Refactor determining of relocation model into methods
1 parent 1bc0447 commit 1798c1a

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

src/librustc_trans/back/write.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use util::fs::link_or_copy;
2424
use errors::{self, Handler, Level, DiagnosticBuilder};
2525
use errors::emitter::Emitter;
2626
use syntax_pos::MultiSpan;
27+
use context::{is_pie_binary, get_reloc_model};
2728

2829
use std::collections::HashMap;
2930
use std::ffi::{CStr, CString};
@@ -154,32 +155,11 @@ fn get_llvm_opt_size(optimize: config::OptLevel) -> llvm::CodeGenOptSize {
154155
}
155156

156157
pub fn create_target_machine(sess: &Session) -> TargetMachineRef {
157-
let reloc_model_arg = match sess.opts.cg.relocation_model {
158-
Some(ref s) => &s[..],
159-
None => &sess.target.target.options.relocation_model[..],
160-
};
161-
let reloc_model = match reloc_model_arg {
162-
"pic" => llvm::RelocPIC,
163-
"static" => llvm::RelocStatic,
164-
"default" => llvm::RelocDefault,
165-
"dynamic-no-pic" => llvm::RelocDynamicNoPic,
166-
_ => {
167-
sess.err(&format!("{:?} is not a valid relocation mode",
168-
sess.opts
169-
.cg
170-
.relocation_model));
171-
sess.abort_if_errors();
172-
bug!();
173-
}
174-
};
158+
let reloc_model = get_reloc_model(sess);
175159

176160
let opt_level = get_llvm_opt_level(sess.opts.optimize);
177161
let use_softfp = sess.opts.cg.soft_float;
178162

179-
let any_library = sess.crate_types.borrow().iter().any(|ty| {
180-
*ty != config::CrateTypeExecutable
181-
});
182-
183163
let ffunction_sections = sess.target.target.options.function_sections;
184164
let fdata_sections = ffunction_sections;
185165

@@ -220,7 +200,7 @@ pub fn create_target_machine(sess: &Session) -> TargetMachineRef {
220200
reloc_model,
221201
opt_level,
222202
use_softfp,
223-
!any_library && reloc_model == llvm::RelocPIC,
203+
is_pie_binary(sess),
224204
ffunction_sections,
225205
fdata_sections,
226206
)

src/librustc_trans/context.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use rustc::ty::subst::{Substs, VecPerParamSpace};
3434
use rustc::ty::{self, Ty, TyCtxt};
3535
use session::config::NoDebugInfo;
3636
use session::Session;
37+
use session::config;
3738
use symbol_map::SymbolMap;
3839
use util::sha2::Sha256;
3940
use util::nodemap::{NodeMap, NodeSet, DefIdMap, FnvHashMap, FnvHashSet};
@@ -322,6 +323,38 @@ impl<'a, 'tcx> Iterator for CrateContextMaybeIterator<'a, 'tcx> {
322323
}
323324
}
324325

326+
pub fn get_reloc_model(sess: &Session) -> llvm::RelocMode {
327+
let reloc_model_arg = match sess.opts.cg.relocation_model {
328+
Some(ref s) => &s[..],
329+
None => &sess.target.target.options.relocation_model[..],
330+
};
331+
332+
match reloc_model_arg {
333+
"pic" => llvm::RelocPIC,
334+
"static" => llvm::RelocStatic,
335+
"default" => llvm::RelocDefault,
336+
"dynamic-no-pic" => llvm::RelocDynamicNoPic,
337+
_ => {
338+
sess.err(&format!("{:?} is not a valid relocation mode",
339+
sess.opts
340+
.cg
341+
.relocation_model));
342+
sess.abort_if_errors();
343+
bug!();
344+
}
345+
}
346+
}
347+
348+
fn is_any_library(sess: &Session) -> bool {
349+
sess.crate_types.borrow().iter().any(|ty| {
350+
*ty != config::CrateTypeExecutable
351+
})
352+
}
353+
354+
pub fn is_pie_binary(sess: &Session) -> bool {
355+
!is_any_library(sess) && get_reloc_model(sess) == llvm::RelocPIC
356+
}
357+
325358
unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextRef, ModuleRef) {
326359
let llcx = llvm::LLVMContextCreate();
327360
let mod_name = CString::new(mod_name).unwrap();
@@ -352,7 +385,11 @@ unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextR
352385
let llvm_target = sess.target.target.llvm_target.as_bytes();
353386
let llvm_target = CString::new(llvm_target).unwrap();
354387
llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
355-
llvm::LLVMRustSetModulePIELevel(llmod);
388+
389+
if is_pie_binary(sess) {
390+
llvm::LLVMRustSetModulePIELevel(llmod);
391+
}
392+
356393
(llcx, llmod)
357394
}
358395

0 commit comments

Comments
 (0)