diff --git a/src/librustc_trans/back/link.rs b/src/librustc_trans/back/link.rs index 4e211d83cff3e..dc894ff9bd0db 100644 --- a/src/librustc_trans/back/link.rs +++ b/src/librustc_trans/back/link.rs @@ -894,18 +894,24 @@ fn link_args(cmd: &mut Linker, let used_link_args = sess.cstore.used_link_args(); + let mut pie = false; if crate_type == config::CrateTypeExecutable && - t.options.position_independent_executables { + t.options.position_independent_executables { let empty_vec = Vec::new(); let args = sess.opts.cg.link_args.as_ref().unwrap_or(&empty_vec); let more_args = &sess.opts.cg.link_arg; let mut args = args.iter().chain(more_args.iter()).chain(used_link_args.iter()); - if get_reloc_model(sess) == llvm::RelocMode::PIC - && !sess.crt_static() && !args.any(|x| *x == "-static") { - cmd.position_independent_executable(); + if get_reloc_model(sess) == llvm::RelocMode::PIC && + !sess.crt_static() && !args.any(|x| *x == "-static") { + pie = true } } + if pie { + cmd.position_independent_executable(); + } else { + cmd.no_position_independent_executable(); + } let relro_level = match sess.opts.debugging_opts.relro_level { Some(level) => level, diff --git a/src/librustc_trans/back/linker.rs b/src/librustc_trans/back/linker.rs index 9b0a5e3f4a5b1..53c49f11c1a68 100644 --- a/src/librustc_trans/back/linker.rs +++ b/src/librustc_trans/back/linker.rs @@ -104,6 +104,7 @@ pub trait Linker { fn add_object(&mut self, path: &Path); fn gc_sections(&mut self, keep_metadata: bool); fn position_independent_executable(&mut self); + fn no_position_independent_executable(&mut self); fn partial_relro(&mut self); fn full_relro(&mut self); fn optimize(&mut self); @@ -178,6 +179,7 @@ impl<'a> Linker for GccLinker<'a> { fn output_filename(&mut self, path: &Path) { self.cmd.arg("-o").arg(path); } fn add_object(&mut self, path: &Path) { self.cmd.arg(path); } fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); } + fn no_position_independent_executable(&mut self) { self.cmd.arg("-no-pie"); } fn partial_relro(&mut self) { self.linker_arg("-z,relro"); } fn full_relro(&mut self) { self.linker_arg("-z,relro,-z,now"); } fn build_static_executable(&mut self) { self.cmd.arg("-static"); } @@ -438,6 +440,10 @@ impl<'a> Linker for MsvcLinker<'a> { // noop } + fn no_position_independent_executable(&mut self) { + // noop + } + fn partial_relro(&mut self) { // noop } @@ -634,6 +640,10 @@ impl<'a> Linker for EmLinker<'a> { // noop } + fn no_position_independent_executable(&mut self) { + // noop + } + fn partial_relro(&mut self) { // noop }