@@ -3,7 +3,7 @@ use std::env;
33use std:: sync:: Arc ;
44use std:: time:: Instant ;
55
6- use gccjit:: { CType , FunctionType , GlobalKind } ;
6+ use gccjit:: { CType , Context , FunctionType , GlobalKind } ;
77use rustc_codegen_ssa:: base:: maybe_create_entry_wrapper;
88use rustc_codegen_ssa:: mono_item:: MonoItemExt ;
99use rustc_codegen_ssa:: traits:: DebugInfoCodegenMethods ;
@@ -15,9 +15,9 @@ use rustc_middle::mir::mono::Visibility;
1515use rustc_middle:: ty:: TyCtxt ;
1616use rustc_session:: config:: DebugInfo ;
1717use rustc_span:: Symbol ;
18- use rustc_target:: spec:: PanicStrategy ;
1918#[ cfg( feature = "master" ) ]
2019use rustc_target:: spec:: SymbolVisibility ;
20+ use rustc_target:: spec:: { PanicStrategy , RelocModel } ;
2121
2222use crate :: builder:: Builder ;
2323use crate :: context:: CodegenCx ;
@@ -151,18 +151,7 @@ pub fn compile_codegen_unit(
151151 } ) ;
152152 }
153153
154- match tcx. sess . relocation_model ( ) {
155- rustc_target:: spec:: RelocModel :: Static => {
156- context. add_command_line_option ( "-fno-pie" ) ;
157- }
158- rustc_target:: spec:: RelocModel :: Pic => {
159- context. add_command_line_option ( "-fPIC" ) ;
160- }
161- rustc_target:: spec:: RelocModel :: Pie => {
162- context. add_command_line_option ( "-fPIE" ) ;
163- }
164- model => eprintln ! ( "Unsupported relocation model: {:?}" , model) ,
165- }
154+ add_pic_option ( & context, tcx. sess . relocation_model ( ) ) ;
166155
167156 let target_cpu = gcc_util:: target_cpu ( tcx. sess ) ;
168157 if target_cpu != "generic" {
@@ -256,6 +245,7 @@ pub fn compile_codegen_unit(
256245 name : cgu_name. to_string ( ) ,
257246 module_llvm : GccContext {
258247 context : Arc :: new ( SyncContext :: new ( context) ) ,
248+ relocation_model : tcx. sess . relocation_model ( ) ,
259249 should_combine_object_files : false ,
260250 temp_dir : None ,
261251 } ,
@@ -265,3 +255,24 @@ pub fn compile_codegen_unit(
265255
266256 ( module, cost)
267257}
258+
259+ pub fn add_pic_option < ' gcc > ( context : & Context < ' gcc > , relocation_model : RelocModel ) {
260+ match relocation_model {
261+ rustc_target:: spec:: RelocModel :: Static => {
262+ context. add_command_line_option ( "-fno-pie" ) ;
263+ context. add_driver_option ( "-fno-pie" ) ;
264+ }
265+ rustc_target:: spec:: RelocModel :: Pic => {
266+ context. add_command_line_option ( "-fPIC" ) ;
267+ // NOTE: we use both add_command_line_option and add_driver_option because the usage in
268+ // this module (compile_codegen_unit) requires add_command_line_option while the usage
269+ // in the back::write module (codegen) requires add_driver_option.
270+ context. add_driver_option ( "-fPIC" ) ;
271+ }
272+ rustc_target:: spec:: RelocModel :: Pie => {
273+ context. add_command_line_option ( "-fPIE" ) ;
274+ context. add_driver_option ( "-fPIE" ) ;
275+ }
276+ model => eprintln ! ( "Unsupported relocation model: {:?}" , model) ,
277+ }
278+ }
0 commit comments