Skip to content

Commit 9350c3c

Browse files
committed
Explicitly pass -no-pie
Some linkers (e.g. Debian and Arch's) that are configured to make PIEs by default will make dynamically linked executables that don't actually dynamically link to anything. Fix that by explicitly passing -no-pie in those cases. Closes #43647
1 parent ed16b0a commit 9350c3c

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/librustc_trans/back/link.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1016,18 +1016,24 @@ fn link_args(cmd: &mut Linker,
10161016

10171017
let used_link_args = sess.cstore.used_link_args();
10181018

1019+
let mut pie = false;
10191020
if crate_type == config::CrateTypeExecutable &&
1020-
t.options.position_independent_executables {
1021+
t.options.position_independent_executables {
10211022
let empty_vec = Vec::new();
10221023
let args = sess.opts.cg.link_args.as_ref().unwrap_or(&empty_vec);
10231024
let more_args = &sess.opts.cg.link_arg;
10241025
let mut args = args.iter().chain(more_args.iter()).chain(used_link_args.iter());
10251026

10261027
if get_reloc_model(sess) == llvm::RelocMode::PIC
10271028
&& !args.any(|x| *x == "-static") {
1028-
cmd.position_independent_executable();
1029+
pie = true
10291030
}
10301031
}
1032+
if pie {
1033+
cmd.position_independent_executable();
1034+
} else {
1035+
cmd.no_position_independent_executable();
1036+
}
10311037

10321038
let relro_level = match sess.opts.debugging_opts.relro_level {
10331039
Some(level) => level,

src/librustc_trans/back/linker.rs

+10
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub trait Linker {
104104
fn add_object(&mut self, path: &Path);
105105
fn gc_sections(&mut self, keep_metadata: bool);
106106
fn position_independent_executable(&mut self);
107+
fn no_position_independent_executable(&mut self);
107108
fn partial_relro(&mut self);
108109
fn full_relro(&mut self);
109110
fn optimize(&mut self);
@@ -177,6 +178,7 @@ impl<'a> Linker for GccLinker<'a> {
177178
fn output_filename(&mut self, path: &Path) { self.cmd.arg("-o").arg(path); }
178179
fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
179180
fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
181+
fn no_position_independent_executable(&mut self) { self.cmd.arg("-no-pie"); }
180182
fn partial_relro(&mut self) { self.linker_arg("-z,relro"); }
181183
fn full_relro(&mut self) { self.linker_arg("-z,relro,-z,now"); }
182184
fn args(&mut self, args: &[String]) { self.cmd.args(args); }
@@ -432,6 +434,10 @@ impl<'a> Linker for MsvcLinker<'a> {
432434
// noop
433435
}
434436

437+
fn no_position_independent_executable(&mut self) {
438+
// noop
439+
}
440+
435441
fn partial_relro(&mut self) {
436442
// noop
437443
}
@@ -628,6 +634,10 @@ impl<'a> Linker for EmLinker<'a> {
628634
// noop
629635
}
630636

637+
fn no_position_independent_executable(&mut self) {
638+
// noop
639+
}
640+
631641
fn partial_relro(&mut self) {
632642
// noop
633643
}

0 commit comments

Comments
 (0)