]> git.lizzy.rs Git - rust.git/commitdiff
Only add cfguard module flag on windows-msvc
authorAndrew Paverd <andrew.paverd@microsoft.com>
Mon, 6 Jul 2020 15:10:42 +0000 (16:10 +0100)
committerAndrew Paverd <andrew.paverd@microsoft.com>
Fri, 10 Jul 2020 08:56:13 +0000 (09:56 +0100)
src/librustc_codegen_llvm/context.rs
src/librustc_codegen_ssa/back/linker.rs
src/test/codegen/cfguard-checks.rs [new file with mode: 0644]
src/test/codegen/cfguard-disabled.rs [new file with mode: 0644]
src/test/codegen/cfguard-nochecks.rs [new file with mode: 0644]
src/test/codegen/cfguard-non-msvc.rs [new file with mode: 0644]
src/test/codegen/cfguard_checks.rs [deleted file]
src/test/codegen/cfguard_disabled.rs [deleted file]
src/test/codegen/cfguard_nochecks.rs [deleted file]
src/test/ui/cfguard-run.rs [new file with mode: 0644]

index d484e15eb2f6e0944ec119192322be79626f8c2b..21ba97d15a485776362716ed8b84b6c946cd1f8d 100644 (file)
@@ -188,14 +188,19 @@ pub unsafe fn create_module(
         llvm::LLVMRustAddModuleFlag(llmod, avoid_plt, 1);
     }
 
-    // Set module flags to enable Windows Control Flow Guard (/guard:cf) metadata
-    // only (`cfguard=1`) or metadata and checks (`cfguard=2`).
-    match sess.opts.debugging_opts.control_flow_guard {
-        CFGuard::Disabled => {}
-        CFGuard::NoChecks => {
-            llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 1)
+    // Control Flow Guard is currently only supported by the MSVC linker on Windows.
+    if sess.target.target.options.is_like_msvc {
+        match sess.opts.debugging_opts.control_flow_guard {
+            CFGuard::Disabled => {}
+            CFGuard::NoChecks => {
+                // Set `cfguard=1` module flag to emit metadata only.
+                llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 1)
+            }
+            CFGuard::Checks => {
+                // Set `cfguard=2` module flag to emit metadata and checks.
+                llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 2)
+            }
         }
-        CFGuard::Checks => llvm::LLVMRustAddModuleFlag(llmod, "cfguard\0".as_ptr() as *const _, 2),
     }
 
     llmod
index 54f55c806d0350c13676056049c8aa39575077ab..550de75c709fb8d194549dac6aaf796bb418b0a4 100644 (file)
@@ -475,9 +475,7 @@ fn pgo_gen(&mut self) {
         self.cmd.arg("__llvm_profile_runtime");
     }
 
-    fn control_flow_guard(&mut self) {
-        self.sess.warn("Windows Control Flow Guard is not supported by this linker.");
-    }
+    fn control_flow_guard(&mut self) {}
 
     fn debuginfo(&mut self, strip: Strip) {
         match strip {
@@ -959,9 +957,7 @@ fn pgo_gen(&mut self) {
         // noop, but maybe we need something like the gnu linker?
     }
 
-    fn control_flow_guard(&mut self) {
-        self.sess.warn("Windows Control Flow Guard is not supported by this linker.");
-    }
+    fn control_flow_guard(&mut self) {}
 
     fn debuginfo(&mut self, _strip: Strip) {
         // Preserve names or generate source maps depending on debug info
@@ -1163,9 +1159,7 @@ fn debuginfo(&mut self, strip: Strip) {
         }
     }
 
-    fn control_flow_guard(&mut self) {
-        self.sess.warn("Windows Control Flow Guard is not supported by this linker.");
-    }
+    fn control_flow_guard(&mut self) {}
 
     fn no_crt_objects(&mut self) {}
 
@@ -1330,9 +1324,7 @@ fn no_crt_objects(&mut self) {}
 
     fn no_default_libraries(&mut self) {}
 
-    fn control_flow_guard(&mut self) {
-        self.sess.warn("Windows Control Flow Guard is not supported by this linker.");
-    }
+    fn control_flow_guard(&mut self) {}
 
     fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType) {}
 
diff --git a/src/test/codegen/cfguard-checks.rs b/src/test/codegen/cfguard-checks.rs
new file mode 100644 (file)
index 0000000..96a0a32
--- /dev/null
@@ -0,0 +1,11 @@
+// compile-flags: -Z control-flow-guard=checks
+// only-msvc
+
+#![crate_type = "lib"]
+
+// A basic test function.
+pub fn test() {
+}
+
+// Ensure the module flag cfguard=2 is present
+// CHECK: !"cfguard", i32 2
diff --git a/src/test/codegen/cfguard-disabled.rs b/src/test/codegen/cfguard-disabled.rs
new file mode 100644 (file)
index 0000000..925e4e8
--- /dev/null
@@ -0,0 +1,11 @@
+// compile-flags: -Z control-flow-guard=no
+// only-msvc
+
+#![crate_type = "lib"]
+
+// A basic test function.
+pub fn test() {
+}
+
+// Ensure the module flag cfguard is not present
+// CHECK-NOT: !"cfguard"
diff --git a/src/test/codegen/cfguard-nochecks.rs b/src/test/codegen/cfguard-nochecks.rs
new file mode 100644 (file)
index 0000000..d7dc3d7
--- /dev/null
@@ -0,0 +1,11 @@
+// compile-flags: -Z control-flow-guard=nochecks
+// only-msvc
+
+#![crate_type = "lib"]
+
+// A basic test function.
+pub fn test() {
+}
+
+// Ensure the module flag cfguard=1 is present
+// CHECK: !"cfguard", i32 1
diff --git a/src/test/codegen/cfguard-non-msvc.rs b/src/test/codegen/cfguard-non-msvc.rs
new file mode 100644 (file)
index 0000000..4008f01
--- /dev/null
@@ -0,0 +1,11 @@
+// compile-flags: -Z control-flow-guard
+// ignore-msvc
+
+#![crate_type = "lib"]
+
+// A basic test function.
+pub fn test() {
+}
+
+// Ensure the cfguard module flag is not added for non-MSVC targets.
+// CHECK-NOT: !"cfguard"
diff --git a/src/test/codegen/cfguard_checks.rs b/src/test/codegen/cfguard_checks.rs
deleted file mode 100644 (file)
index 96f9158..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-// compile-flags: -Z control-flow-guard=checks
-
-#![crate_type = "lib"]
-
-// A basic test function.
-pub fn test() {
-}
-
-// Ensure the module flag cfguard=2 is present
-// CHECK: !"cfguard", i32 2
diff --git a/src/test/codegen/cfguard_disabled.rs b/src/test/codegen/cfguard_disabled.rs
deleted file mode 100644 (file)
index 1325ffc..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-// compile-flags: -Z control-flow-guard=no
-
-#![crate_type = "lib"]
-
-// A basic test function.
-pub fn test() {
-}
-
-// Ensure the module flag cfguard is not present
-// CHECK-NOT: !"cfguard"
diff --git a/src/test/codegen/cfguard_nochecks.rs b/src/test/codegen/cfguard_nochecks.rs
deleted file mode 100644 (file)
index ae1de4c..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-// compile-flags: -Z control-flow-guard=nochecks
-
-#![crate_type = "lib"]
-
-// A basic test function.
-pub fn test() {
-}
-
-// Ensure the module flag cfguard=1 is present
-// CHECK: !"cfguard", i32 1
diff --git a/src/test/ui/cfguard-run.rs b/src/test/ui/cfguard-run.rs
new file mode 100644 (file)
index 0000000..21368fa
--- /dev/null
@@ -0,0 +1,6 @@
+// run-pass
+// compile-flags: -Z control-flow-guard
+
+pub fn main() {
+    println!("hello, world");
+}