Skip to content

Commit 1d838a2

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 rust-lang#43647
1 parent 560b6ca commit 1d838a2

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/librustc_trans/back/link.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -894,18 +894,24 @@ fn link_args(cmd: &mut Linker,
894894

895895
let used_link_args = sess.cstore.used_link_args();
896896

897+
let mut pie = false;
897898
if crate_type == config::CrateTypeExecutable &&
898-
t.options.position_independent_executables {
899+
t.options.position_independent_executables {
899900
let empty_vec = Vec::new();
900901
let args = sess.opts.cg.link_args.as_ref().unwrap_or(&empty_vec);
901902
let more_args = &sess.opts.cg.link_arg;
902903
let mut args = args.iter().chain(more_args.iter()).chain(used_link_args.iter());
903904

904-
if get_reloc_model(sess) == llvm::RelocMode::PIC
905-
&& !sess.crt_static() && !args.any(|x| *x == "-static") {
906-
cmd.position_independent_executable();
905+
if get_reloc_model(sess) == llvm::RelocMode::PIC &&
906+
!sess.crt_static() && !args.any(|x| *x == "-static") {
907+
pie = true
907908
}
908909
}
910+
if pie {
911+
cmd.position_independent_executable();
912+
} else {
913+
cmd.no_position_independent_executable();
914+
}
909915

910916
let relro_level = match sess.opts.debugging_opts.relro_level {
911917
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);
@@ -178,6 +179,7 @@ impl<'a> Linker for GccLinker<'a> {
178179
fn output_filename(&mut self, path: &Path) { self.cmd.arg("-o").arg(path); }
179180
fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
180181
fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
182+
fn no_position_independent_executable(&mut self) { self.cmd.arg("-no-pie"); }
181183
fn partial_relro(&mut self) { self.linker_arg("-z,relro"); }
182184
fn full_relro(&mut self) { self.linker_arg("-z,relro,-z,now"); }
183185
fn build_static_executable(&mut self) { self.cmd.arg("-static"); }
@@ -438,6 +440,10 @@ impl<'a> Linker for MsvcLinker<'a> {
438440
// noop
439441
}
440442

443+
fn no_position_independent_executable(&mut self) {
444+
// noop
445+
}
446+
441447
fn partial_relro(&mut self) {
442448
// noop
443449
}
@@ -634,6 +640,10 @@ impl<'a> Linker for EmLinker<'a> {
634640
// noop
635641
}
636642

643+
fn no_position_independent_executable(&mut self) {
644+
// noop
645+
}
646+
637647
fn partial_relro(&mut self) {
638648
// noop
639649
}

0 commit comments

Comments
 (0)