linker: Use data execution prevention options by default when linker supports them

This commit is contained in:
Vadim Petrochenkov 2021-03-28 23:18:39 +03:00
parent 4a20eb6a9d
commit cc5392e76b
11 changed files with 23 additions and 73 deletions

View file

@ -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.

View file

@ -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> {