[lld] [LLD][COFF] Set __guard_flags to CF_INSTRUMENTED if any object is instrumented (PR #115374)

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 7 13:46:25 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld-coff

Author: Jacek Caban (cjacek)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/115374.diff


2 Files Affected:

- (modified) lld/COFF/Writer.cpp (+15-2) 
- (added) lld/test/COFF/cfguard-off-instrumented.s (+22) 


``````````diff
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 71ee5ce4685553..58d0700c52aaf4 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -1217,8 +1217,7 @@ void Writer::createMiscChunks() {
     createSEHTable();
 
   // Create /guard:cf tables if requested.
-  if (config->guardCF != GuardCFLevel::Off)
-    createGuardCFTables();
+  createGuardCFTables();
 
   if (isArm64EC(config->machine))
     createECChunks();
@@ -1979,6 +1978,20 @@ void Writer::markSymbolsWithRelocations(ObjFile *file,
 void Writer::createGuardCFTables() {
   Configuration *config = &ctx.config;
 
+  if (config->guardCF == GuardCFLevel::Off) {
+    // MSVC marks the entire image as instrumented if any input object was built
+    // with /guard:cf.
+    for (ObjFile *file : ctx.objFileInstances) {
+      if (file->hasGuardCF()) {
+        Symbol *flagSym = ctx.symtab.findUnderscore("__guard_flags");
+        cast<DefinedAbsolute>(flagSym)->setVA(
+            uint32_t(GuardFlags::CF_INSTRUMENTED));
+        break;
+      }
+    }
+    return;
+  }
+
   SymbolRVASet addressTakenSyms;
   SymbolRVASet giatsRVASet;
   std::vector<Symbol *> giatsSymbols;
diff --git a/lld/test/COFF/cfguard-off-instrumented.s b/lld/test/COFF/cfguard-off-instrumented.s
new file mode 100644
index 00000000000000..4bd81d99568927
--- /dev/null
+++ b/lld/test/COFF/cfguard-off-instrumented.s
@@ -0,0 +1,22 @@
+// Verify that __guard_flags is set to CF_INSTRUMENTED if CF guard is disabled,
+// but the input object was built with CF guard.
+
+// REQUIRES: x86
+
+// RUN: llvm-mc -filetype=obj -triple=x86_64-windows %s -o %t.obj
+// RUN: lld-link -out:%t1.dll %t.obj -dll -noentry
+// RUN: lld-link -out:%t2.dll %t.obj -dll -noentry -guard:no
+
+// RUN: llvm-readobj --hex-dump=.test %t1.dll | FileCheck %s
+// RUN: llvm-readobj --hex-dump=.test %t2.dll | FileCheck %s
+// CHECK: 0x180001000 00010000
+
+        .def     @feat.00;
+        .scl    3;
+        .type   0;
+        .endef
+        .globl  @feat.00
+ at feat.00 = 0x800
+
+        .section .test, "r"
+        .long __guard_flags

``````````

</details>


https://github.com/llvm/llvm-project/pull/115374


More information about the llvm-commits mailing list