Skip to content

Commit 198d907

Browse files
committed
Add pie as another relocation-model value
1 parent 497ee32 commit 198d907

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
@@ -190,11 +190,14 @@ pub unsafe fn create_module(
190190
let llvm_target = SmallCStr::new(&sess.target.llvm_target);
191191
llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
192192

193-
if sess.relocation_model() == RelocModel::Pic {
193+
let reloc_model = sess.relocation_model();
194+
if matches!(reloc_model, RelocModel::Pic | RelocModel::Pie) {
194195
llvm::LLVMRustSetModulePICLevel(llmod);
195196
// PIE is potentially more effective than PIC, but can only be used in executables.
196197
// If all our outputs are executables, then we can relax PIC to PIE.
197-
if sess.crate_types().iter().all(|ty| *ty == CrateType::Executable) {
198+
if reloc_model == RelocModel::Pie
199+
|| sess.crate_types().iter().all(|ty| *ty == CrateType::Executable)
200+
{
198201
llvm::LLVMRustSetModulePIELevel(llmod);
199202
}
200203
}

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
@@ -144,6 +144,12 @@ impl CodegenCx<'ll, 'tcx> {
144144
return true;
145145
}
146146

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

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
@@ -287,6 +287,7 @@ impl ToJson for MergeFunctions {
287287
pub enum RelocModel {
288288
Static,
289289
Pic,
290+
Pie,
290291
DynamicNoPic,
291292
Ropi,
292293
Rwpi,
@@ -300,6 +301,7 @@ impl FromStr for RelocModel {
300301
Ok(match s {
301302
"static" => RelocModel::Static,
302303
"pic" => RelocModel::Pic,
304+
"pie" => RelocModel::Pie,
303305
"dynamic-no-pic" => RelocModel::DynamicNoPic,
304306
"ropi" => RelocModel::Ropi,
305307
"rwpi" => RelocModel::Rwpi,
@@ -314,6 +316,7 @@ impl ToJson for RelocModel {
314316
match *self {
315317
RelocModel::Static => "static",
316318
RelocModel::Pic => "pic",
319+
RelocModel::Pie => "pie",
317320
RelocModel::DynamicNoPic => "dynamic-no-pic",
318321
RelocModel::Ropi => "ropi",
319322
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)