@@ -7,7 +7,7 @@ use crate::back::profiling::{
77use crate :: base;
88use crate :: common;
99use crate :: consts;
10- use crate :: context:: { get_reloc_model , is_pie_binary } ;
10+ use crate :: context:: all_outputs_are_pic_executables ;
1111use crate :: llvm:: { self , DiagnosticInfo , PassManager , SMDiagnostic } ;
1212use crate :: llvm_util;
1313use crate :: type_:: Type ;
@@ -25,6 +25,7 @@ use rustc_middle::bug;
2525use rustc_middle:: ty:: TyCtxt ;
2626use rustc_session:: config:: { self , Lto , OutputType , Passes , Sanitizer , SwitchWithOptPath } ;
2727use rustc_session:: Session ;
28+ use rustc_target:: spec:: RelocModel ;
2829
2930use libc:: { c_char, c_int, c_uint, c_void, size_t} ;
3031use std:: ffi:: CString ;
@@ -35,16 +36,6 @@ use std::slice;
3536use std:: str;
3637use std:: sync:: Arc ;
3738
38- pub const RELOC_MODEL_ARGS : [ ( & str , llvm:: RelocMode ) ; 7 ] = [
39- ( "pic" , llvm:: RelocMode :: PIC ) ,
40- ( "static" , llvm:: RelocMode :: Static ) ,
41- ( "default" , llvm:: RelocMode :: Default ) ,
42- ( "dynamic-no-pic" , llvm:: RelocMode :: DynamicNoPic ) ,
43- ( "ropi" , llvm:: RelocMode :: ROPI ) ,
44- ( "rwpi" , llvm:: RelocMode :: RWPI ) ,
45- ( "ropi-rwpi" , llvm:: RelocMode :: ROPI_RWPI ) ,
46- ] ;
47-
4839pub const CODE_GEN_MODEL_ARGS : & [ ( & str , llvm:: CodeModel ) ] = & [
4940 ( "small" , llvm:: CodeModel :: Small ) ,
5041 ( "kernel" , llvm:: CodeModel :: Kernel ) ,
@@ -84,19 +75,13 @@ pub fn write_output_file(
8475 }
8576}
8677
87- pub fn create_informational_target_machine (
88- sess : & Session ,
89- find_features : bool ,
90- ) -> & ' static mut llvm:: TargetMachine {
91- target_machine_factory ( sess, config:: OptLevel :: No , find_features) ( )
78+ pub fn create_informational_target_machine ( sess : & Session ) -> & ' static mut llvm:: TargetMachine {
79+ target_machine_factory ( sess, config:: OptLevel :: No ) ( )
9280 . unwrap_or_else ( |err| llvm_err ( sess. diagnostic ( ) , & err) . raise ( ) )
9381}
9482
95- pub fn create_target_machine (
96- tcx : TyCtxt < ' _ > ,
97- find_features : bool ,
98- ) -> & ' static mut llvm:: TargetMachine {
99- target_machine_factory ( & tcx. sess , tcx. backend_optimization_level ( LOCAL_CRATE ) , find_features) ( )
83+ pub fn create_target_machine ( tcx : TyCtxt < ' _ > ) -> & ' static mut llvm:: TargetMachine {
84+ target_machine_factory ( & tcx. sess , tcx. backend_optimization_level ( LOCAL_CRATE ) ) ( )
10085 . unwrap_or_else ( |err| llvm_err ( tcx. sess . diagnostic ( ) , & err) . raise ( ) )
10186}
10287
@@ -126,15 +111,22 @@ fn to_pass_builder_opt_level(cfg: config::OptLevel) -> llvm::PassBuilderOptLevel
126111 }
127112}
128113
129- // If find_features is true this won't access `sess.crate_types` by assuming
130- // that `is_pie_binary` is false. When we discover LLVM target features
131- // `sess.crate_types` is uninitialized so we cannot access it.
114+ fn to_llvm_relocation_model ( relocation_model : RelocModel ) -> llvm:: RelocModel {
115+ match relocation_model {
116+ RelocModel :: Static => llvm:: RelocModel :: Static ,
117+ RelocModel :: Pic => llvm:: RelocModel :: PIC ,
118+ RelocModel :: DynamicNoPic => llvm:: RelocModel :: DynamicNoPic ,
119+ RelocModel :: Ropi => llvm:: RelocModel :: ROPI ,
120+ RelocModel :: Rwpi => llvm:: RelocModel :: RWPI ,
121+ RelocModel :: RopiRwpi => llvm:: RelocModel :: ROPI_RWPI ,
122+ }
123+ }
124+
132125pub fn target_machine_factory (
133126 sess : & Session ,
134127 optlvl : config:: OptLevel ,
135- find_features : bool ,
136128) -> Arc < dyn Fn ( ) -> Result < & ' static mut llvm:: TargetMachine , String > + Send + Sync > {
137- let reloc_model = get_reloc_model ( sess) ;
129+ let reloc_model = to_llvm_relocation_model ( sess. relocation_model ( ) ) ;
138130
139131 let ( opt_level, _) = to_llvm_opt_settings ( optlvl) ;
140132 let use_softfp = sess. opts . cg . soft_float ;
@@ -175,7 +167,7 @@ pub fn target_machine_factory(
175167 let features = features. join ( "," ) ;
176168 let features = CString :: new ( features) . unwrap ( ) ;
177169 let abi = SmallCStr :: new ( & sess. target . target . options . llvm_abiname ) ;
178- let is_pie_binary = !find_features && is_pie_binary ( sess) ;
170+ let pic_is_pie = all_outputs_are_pic_executables ( sess) ;
179171 let trap_unreachable = sess. target . target . options . trap_unreachable ;
180172 let emit_stack_size_section = sess. opts . debugging_opts . emit_stack_sizes ;
181173
@@ -192,7 +184,7 @@ pub fn target_machine_factory(
192184 reloc_model,
193185 opt_level,
194186 use_softfp,
195- is_pie_binary ,
187+ pic_is_pie ,
196188 ffunction_sections,
197189 fdata_sections,
198190 trap_unreachable,
0 commit comments