Skip to content

Commit 6f1e930

Browse files
authored
Rollup merge of #88820 - hlopko:add_pie_relocation_model, r=petrochenkov
Add `pie` as another `relocation-model` value MCP: rust-lang/compiler-team#461
2 parents 37df275 + 198d907 commit 6f1e930

File tree

11 files changed

+147
-8
lines changed

11 files changed

+147
-8
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ fn to_pass_builder_opt_level(cfg: config::OptLevel) -> llvm::PassBuilderOptLevel
129129
fn to_llvm_relocation_model(relocation_model: RelocModel) -> llvm::RelocModel {
130130
match relocation_model {
131131
RelocModel::Static => llvm::RelocModel::Static,
132-
RelocModel::Pic => llvm::RelocModel::PIC,
132+
// LLVM doesn't have a PIE relocation model, it represents PIE as PIC with an extra attribute.
133+
RelocModel::Pic | RelocModel::Pie => llvm::RelocModel::PIC,
133134
RelocModel::DynamicNoPic => llvm::RelocModel::DynamicNoPic,
134135
RelocModel::Ropi => llvm::RelocModel::ROPI,
135136
RelocModel::Rwpi => llvm::RelocModel::RWPI,

compiler/rustc_codegen_llvm/src/context.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,14 @@ pub unsafe fn create_module(
195195
let llvm_target = SmallCStr::new(&sess.target.llvm_target);
196196
llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
197197

198-
if sess.relocation_model() == RelocModel::Pic {
198+
let reloc_model = sess.relocation_model();
199+
if matches!(reloc_model, RelocModel::Pic | RelocModel::Pie) {
199200
llvm::LLVMRustSetModulePICLevel(llmod);
200201
// PIE is potentially more effective than PIC, but can only be used in executables.
201202
// If all our outputs are executables, then we can relax PIC to PIE.
202-
if sess.crate_types().iter().all(|ty| *ty == CrateType::Executable) {
203+
if reloc_model == RelocModel::Pie
204+
|| sess.crate_types().iter().all(|ty| *ty == CrateType::Executable)
205+
{
203206
llvm::LLVMRustSetModulePIELevel(llmod);
204207
}
205208
}

compiler/rustc_codegen_llvm/src/lib.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,16 @@ impl CodegenBackend for LlvmCodegenBackend {
211211
match req {
212212
PrintRequest::RelocationModels => {
213213
println!("Available relocation models:");
214-
for name in
215-
&["static", "pic", "dynamic-no-pic", "ropi", "rwpi", "ropi-rwpi", "default"]
216-
{
214+
for name in &[
215+
"static",
216+
"pic",
217+
"pie",
218+
"dynamic-no-pic",
219+
"ropi",
220+
"rwpi",
221+
"ropi-rwpi",
222+
"default",
223+
] {
217224
println!(" {}", name);
218225
}
219226
println!();

compiler/rustc_codegen_llvm/src/mono_item.rs

+6
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ impl CodegenCx<'ll, 'tcx> {
143143
return true;
144144
}
145145

146+
// With pie relocation model calls of functions defined in the translation
147+
// unit can use copy relocations.
148+
if self.tcx.sess.relocation_model() == RelocModel::Pie && !is_declaration {
149+
return true;
150+
}
151+
146152
return false;
147153
}
148154
}

compiler/rustc_codegen_ssa/src/back/link.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1490,9 +1490,13 @@ fn exec_linker(
14901490
fn link_output_kind(sess: &Session, crate_type: CrateType) -> LinkOutputKind {
14911491
let kind = match (crate_type, sess.crt_static(Some(crate_type)), sess.relocation_model()) {
14921492
(CrateType::Executable, _, _) if sess.is_wasi_reactor() => LinkOutputKind::WasiReactorExe,
1493-
(CrateType::Executable, false, RelocModel::Pic) => LinkOutputKind::DynamicPicExe,
1493+
(CrateType::Executable, false, RelocModel::Pic | RelocModel::Pie) => {
1494+
LinkOutputKind::DynamicPicExe
1495+
}
14941496
(CrateType::Executable, false, _) => LinkOutputKind::DynamicNoPicExe,
1495-
(CrateType::Executable, true, RelocModel::Pic) => LinkOutputKind::StaticPicExe,
1497+
(CrateType::Executable, true, RelocModel::Pic | RelocModel::Pie) => {
1498+
LinkOutputKind::StaticPicExe
1499+
}
14961500
(CrateType::Executable, true, _) => LinkOutputKind::StaticNoPicExe,
14971501
(_, true, _) => LinkOutputKind::StaticDylib,
14981502
(_, false, _) => LinkOutputKind::DynamicDylib,

compiler/rustc_target/src/spec/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ impl ToJson for MergeFunctions {
288288
pub enum RelocModel {
289289
Static,
290290
Pic,
291+
Pie,
291292
DynamicNoPic,
292293
Ropi,
293294
Rwpi,
@@ -301,6 +302,7 @@ impl FromStr for RelocModel {
301302
Ok(match s {
302303
"static" => RelocModel::Static,
303304
"pic" => RelocModel::Pic,
305+
"pie" => RelocModel::Pie,
304306
"dynamic-no-pic" => RelocModel::DynamicNoPic,
305307
"ropi" => RelocModel::Ropi,
306308
"rwpi" => RelocModel::Rwpi,
@@ -315,6 +317,7 @@ impl ToJson for RelocModel {
315317
match *self {
316318
RelocModel::Static => "static",
317319
RelocModel::Pic => "pic",
320+
RelocModel::Pie => "pie",
318321
RelocModel::DynamicNoPic => "dynamic-no-pic",
319322
RelocModel::Ropi => "ropi",
320323
RelocModel::Rwpi => "rwpi",

src/doc/rustc/src/codegen-options/index.md

+4
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ Equivalent to the "uppercase" `-fPIC` or `-fPIE` options in other compilers,
435435
depending on the produced crate types. \
436436
This is the default model for majority of supported targets.
437437

438+
- `pie` - position independent executable, relocatable code but without support for symbol
439+
interpositioning (replacing symbols by name using `LD_PRELOAD` and similar). Equivalent to the "uppercase" `-fPIE` option in other compilers. `pie`
440+
code cannot be linked into shared libraries (you'll get a linking error on attempt to do this).
441+
438442
#### Special relocation models
439443

440444
- `dynamic-no-pic` - relocatable external references, non-relocatable code. \
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// revisions: x64
2+
// assembly-output: emit-asm
3+
// [x64] compile-flags: --target x86_64-unknown-linux-gnu -Crelocation-model=pic
4+
// [x64] needs-llvm-components: x86
5+
6+
7+
#![feature(no_core, lang_items)]
8+
#![no_core]
9+
#![crate_type="rlib"]
10+
11+
#[lang = "sized"]
12+
trait Sized {}
13+
#[lang = "copy"]
14+
trait Copy {}
15+
16+
// CHECK-LABEL: call_other_fn:
17+
// CHECK: {{(jmpq|callq)}} *other_fn@GOTPCREL(%rip)
18+
#[no_mangle]
19+
pub fn call_other_fn() -> u8 {
20+
unsafe {
21+
other_fn()
22+
}
23+
}
24+
25+
// CHECK-LABEL: other_fn:
26+
// CHECK: callq *foreign_fn@GOTPCREL(%rip)
27+
#[no_mangle]
28+
#[inline(never)]
29+
pub fn other_fn() -> u8 {
30+
unsafe {
31+
foreign_fn()
32+
}
33+
}
34+
35+
extern "C" {fn foreign_fn() -> u8;}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// revisions: x64
2+
// assembly-output: emit-asm
3+
// [x64] compile-flags: --target x86_64-unknown-linux-gnu -Crelocation-model=pie
4+
// [x64] needs-llvm-components: x86
5+
6+
7+
#![feature(no_core, lang_items)]
8+
#![no_core]
9+
#![crate_type="rlib"]
10+
11+
#[lang = "sized"]
12+
trait Sized {}
13+
#[lang = "copy"]
14+
trait Copy {}
15+
16+
// CHECK-LABEL: call_other_fn:
17+
// With PIE local functions are called "directly".
18+
// CHECK: {{(jmp|callq)}} other_fn
19+
#[no_mangle]
20+
pub fn call_other_fn() -> u8 {
21+
unsafe {
22+
other_fn()
23+
}
24+
}
25+
26+
// CHECK-LABEL: other_fn:
27+
// External functions are still called through GOT, since we don't know if the symbol
28+
// is defined in the binary or in the shared library.
29+
// CHECK: callq *foreign_fn@GOTPCREL(%rip)
30+
#[no_mangle]
31+
#[inline(never)]
32+
pub fn other_fn() -> u8 {
33+
unsafe {
34+
foreign_fn()
35+
}
36+
}
37+
38+
extern "C" {fn foreign_fn() -> u8;}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-flags: -C relocation-model=pic
2+
3+
#![crate_type = "rlib"]
4+
5+
// CHECK: define i8 @call_foreign_fn()
6+
#[no_mangle]
7+
pub fn call_foreign_fn() -> u8 {
8+
unsafe {
9+
foreign_fn()
10+
}
11+
}
12+
13+
// CHECK: declare zeroext i8 @foreign_fn()
14+
extern "C" {fn foreign_fn() -> u8;}
15+
16+
// CHECK: !{i32 7, !"PIC Level", i32 2}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// compile-flags: -C relocation-model=pie
2+
// only-x86_64-unknown-linux-gnu
3+
4+
#![crate_type = "rlib"]
5+
6+
// With PIE we know local functions cannot be interpositioned, we can mark them
7+
// as dso_local.
8+
// CHECK: define dso_local i8 @call_foreign_fn()
9+
#[no_mangle]
10+
pub fn call_foreign_fn() -> u8 {
11+
unsafe {
12+
foreign_fn()
13+
}
14+
}
15+
16+
// External functions are still marked as non-dso_local, since we don't know if the symbol
17+
// is defined in the binary or in the shared library.
18+
// CHECK: declare zeroext i8 @foreign_fn()
19+
extern "C" {fn foreign_fn() -> u8;}
20+
21+
// CHECK: !{i32 7, !"PIC Level", i32 2}
22+
// CHECK: !{i32 7, !"PIE Level", i32 2}

0 commit comments

Comments
 (0)