Skip to content

Commit 9af5181

Browse files
committed
Correctly handle the relocation model for LTO
1 parent 71ae65f commit 9af5181

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

src/back/lto.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use rustc_middle::bug;
3535
use rustc_middle::dep_graph::WorkProduct;
3636
use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
3737
use rustc_session::config::{CrateType, Lto};
38+
use rustc_target::spec::RelocModel;
3839
use tempfile::{TempDir, tempdir};
3940

4041
use crate::back::write::save_temp_bitcode;
@@ -632,7 +633,13 @@ pub unsafe fn optimize_thin_module(
632633
}
633634
};
634635
let module = ModuleCodegen {
635-
module_llvm: GccContext { context, should_combine_object_files, temp_dir: None },
636+
module_llvm: GccContext {
637+
context,
638+
should_combine_object_files,
639+
// TODO(antoyo): use the correct relocation model here.
640+
relocation_model: RelocModel::Pic,
641+
temp_dir: None,
642+
},
636643
name: thin_module.name().to_string(),
637644
kind: ModuleKind::Regular,
638645
};

src/back/write.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_session::config::OutputType;
1010
use rustc_span::fatal_error::FatalError;
1111
use rustc_target::spec::SplitDebuginfo;
1212

13+
use crate::base::add_pic_option;
1314
use crate::errors::CopyBitcode;
1415
use crate::{GccCodegenBackend, GccContext};
1516

@@ -159,7 +160,7 @@ pub(crate) unsafe fn codegen(
159160

160161
// NOTE: without -fuse-linker-plugin, we get the following error:
161162
// lto1: internal compiler error: decompressed stream: Destination buffer is too small
162-
// TODO: since we do not do LTO when the linker is invoked anymore, perhaps
163+
// TODO(antoyo): since we do not do LTO when the linker is invoked anymore, perhaps
163164
// the following flag is not necessary anymore.
164165
context.add_driver_option("-fuse-linker-plugin");
165166
}
@@ -181,10 +182,9 @@ pub(crate) unsafe fn codegen(
181182
// This option is to mute it to make the UI tests pass with LTO enabled.
182183
context.add_driver_option("-Wno-lto-type-mismatch");
183184
// NOTE: this doesn't actually generate an executable. With the above flags, it combines the .o files together in another .o.
184-
// TODO: can this be switched back to OutputKind::Object?
185185
context.compile_to_file(OutputKind::Executable, &lto_path);
186186

187-
let context = Context::default(); // TODO: might need to set some other flags from new_context.
187+
let context = Context::default();
188188
if cgcx.target_arch == "x86" || cgcx.target_arch == "x86_64" {
189189
// NOTE: it seems we need to use add_driver_option instead of
190190
// add_command_line_option here because we use the LTO frontend via gcc.
@@ -198,7 +198,7 @@ pub(crate) unsafe fn codegen(
198198
context.add_driver_option("-x");
199199
context.add_driver_option("lto");
200200
}
201-
context.add_driver_option("-fPIC"); // TODO TODO: only add this flag when necessary.
201+
add_pic_option(&context, module.module_llvm.relocation_model);
202202
context.add_driver_option(lto_path);
203203

204204
context.compile_to_file(OutputKind::ObjectFile, path);

src/base.rs

+25-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::env;
33
use std::sync::Arc;
44
use std::time::Instant;
55

6-
use gccjit::{CType, FunctionType, GlobalKind};
6+
use gccjit::{CType, Context, FunctionType, GlobalKind};
77
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
88
use rustc_codegen_ssa::mono_item::MonoItemExt;
99
use rustc_codegen_ssa::traits::DebugInfoCodegenMethods;
@@ -15,9 +15,9 @@ use rustc_middle::mir::mono::Visibility;
1515
use rustc_middle::ty::TyCtxt;
1616
use rustc_session::config::DebugInfo;
1717
use rustc_span::Symbol;
18-
use rustc_target::spec::PanicStrategy;
1918
#[cfg(feature = "master")]
2019
use rustc_target::spec::SymbolVisibility;
20+
use rustc_target::spec::{PanicStrategy, RelocModel};
2121

2222
use crate::builder::Builder;
2323
use 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+
}

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ use rustc_session::Session;
113113
use rustc_session::config::{Lto, OptLevel, OutputFilenames};
114114
use rustc_span::Symbol;
115115
use rustc_span::fatal_error::FatalError;
116+
use rustc_target::spec::RelocModel;
116117
use tempfile::TempDir;
117118

118119
use crate::back::lto::ModuleBuffer;
@@ -298,6 +299,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
298299
) -> Self::Module {
299300
let mut mods = GccContext {
300301
context: Arc::new(SyncContext::new(new_context(tcx))),
302+
relocation_model: tcx.sess.relocation_model(),
301303
should_combine_object_files: false,
302304
temp_dir: None,
303305
};
@@ -329,6 +331,7 @@ impl ExtraBackendMethods for GccCodegenBackend {
329331

330332
pub struct GccContext {
331333
context: Arc<SyncContext>,
334+
relocation_model: RelocModel,
332335
should_combine_object_files: bool,
333336
// Temporary directory used by LTO. We keep it here so that it's not removed before linking.
334337
temp_dir: Option<TempDir>,

0 commit comments

Comments
 (0)