@@ -3,7 +3,7 @@ use std::env;
3
3
use std:: sync:: Arc ;
4
4
use std:: time:: Instant ;
5
5
6
- use gccjit:: { CType , FunctionType , GlobalKind } ;
6
+ use gccjit:: { CType , Context , FunctionType , GlobalKind } ;
7
7
use rustc_codegen_ssa:: base:: maybe_create_entry_wrapper;
8
8
use rustc_codegen_ssa:: mono_item:: MonoItemExt ;
9
9
use rustc_codegen_ssa:: traits:: DebugInfoCodegenMethods ;
@@ -15,9 +15,9 @@ use rustc_middle::mir::mono::Visibility;
15
15
use rustc_middle:: ty:: TyCtxt ;
16
16
use rustc_session:: config:: DebugInfo ;
17
17
use rustc_span:: Symbol ;
18
- use rustc_target:: spec:: PanicStrategy ;
19
18
#[ cfg( feature = "master" ) ]
20
19
use rustc_target:: spec:: SymbolVisibility ;
20
+ use rustc_target:: spec:: { PanicStrategy , RelocModel } ;
21
21
22
22
use crate :: builder:: Builder ;
23
23
use crate :: context:: CodegenCx ;
@@ -151,18 +151,7 @@ pub fn compile_codegen_unit(
151
151
} ) ;
152
152
}
153
153
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 ( ) ) ;
166
155
167
156
let target_cpu = gcc_util:: target_cpu ( tcx. sess ) ;
168
157
if target_cpu != "generic" {
@@ -256,6 +245,7 @@ pub fn compile_codegen_unit(
256
245
name : cgu_name. to_string ( ) ,
257
246
module_llvm : GccContext {
258
247
context : Arc :: new ( SyncContext :: new ( context) ) ,
248
+ relocation_model : tcx. sess . relocation_model ( ) ,
259
249
should_combine_object_files : false ,
260
250
temp_dir : None ,
261
251
} ,
@@ -265,3 +255,24 @@ pub fn compile_codegen_unit(
265
255
266
256
( module, cost)
267
257
}
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