From cc5392e76bb164a8a08bf7c4e99fdf0bf339193a Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 28 Mar 2021 23:18:39 +0300 Subject: linker: Use data execution prevention options by default when linker supports them --- compiler/rustc_codegen_ssa/src/back/link.rs | 4 ++++ compiler/rustc_codegen_ssa/src/back/linker.rs | 13 +++++++++++++ 2 files changed, 17 insertions(+) (limited to 'compiler/rustc_codegen_ssa/src') diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index e7938fe8af9..686ebc13ea3 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -1651,6 +1651,10 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>( cmd.add_eh_frame_header(); } + // NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER + // Make the binary compatible with data execution prevention schemes. + cmd.add_no_exec(); + // NO-OPT-OUT, OBJECT-FILES-NO // Avoid linking to dynamic libraries unless they satisfy some undefined symbols // at the point at which they are specified on the command line. diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 592675d916a..e19274e579b 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -130,6 +130,7 @@ pub trait Linker { fn group_end(&mut self); fn linker_plugin_lto(&mut self); fn add_eh_frame_header(&mut self) {} + fn add_no_exec(&mut self) {} fn add_as_needed(&mut self) {} fn finalize(&mut self); } @@ -643,6 +644,14 @@ impl<'a> Linker for GccLinker<'a> { self.linker_arg("--eh-frame-hdr"); } + fn add_no_exec(&mut self) { + if self.sess.target.is_like_windows { + self.linker_arg("--nxcompat"); + } else if self.sess.target.linker_is_gnu { + self.linker_arg("-znoexecstack"); + } + } + fn add_as_needed(&mut self) { if self.sess.target.linker_is_gnu { self.linker_arg("--as-needed"); @@ -885,6 +894,10 @@ impl<'a> Linker for MsvcLinker<'a> { fn linker_plugin_lto(&mut self) { // Do nothing } + + fn add_no_exec(&mut self) { + self.cmd.arg("/NXCOMPAT"); + } } pub struct EmLinker<'a> { -- cgit 1.4.1-3-g733a5