@@ -34,6 +34,7 @@ use rustc::ty::subst::{Substs, VecPerParamSpace};
34
34
use rustc:: ty:: { self , Ty , TyCtxt } ;
35
35
use session:: config:: NoDebugInfo ;
36
36
use session:: Session ;
37
+ use session:: config;
37
38
use symbol_map:: SymbolMap ;
38
39
use util:: sha2:: Sha256 ;
39
40
use util:: nodemap:: { NodeMap , NodeSet , DefIdMap , FnvHashMap , FnvHashSet } ;
@@ -322,6 +323,38 @@ impl<'a, 'tcx> Iterator for CrateContextMaybeIterator<'a, 'tcx> {
322
323
}
323
324
}
324
325
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
+
325
358
unsafe fn create_context_and_module ( sess : & Session , mod_name : & str ) -> ( ContextRef , ModuleRef ) {
326
359
let llcx = llvm:: LLVMContextCreate ( ) ;
327
360
let mod_name = CString :: new ( mod_name) . unwrap ( ) ;
@@ -352,7 +385,11 @@ unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextR
352
385
let llvm_target = sess. target . target . llvm_target . as_bytes ( ) ;
353
386
let llvm_target = CString :: new ( llvm_target) . unwrap ( ) ;
354
387
llvm:: LLVMRustSetNormalizedTarget ( llmod, llvm_target. as_ptr ( ) ) ;
355
- llvm:: LLVMRustSetModulePIELevel ( llmod) ;
388
+
389
+ if is_pie_binary ( sess) {
390
+ llvm:: LLVMRustSetModulePIELevel ( llmod) ;
391
+ }
392
+
356
393
( llcx, llmod)
357
394
}
358
395
0 commit comments