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

via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 8 05:01:28 PST 2024


Author: Jacek Caban
Date: 2024-11-08T14:01:25+01:00
New Revision: 5fbe9b958dc3035480406c2cd4524e4827d2dfaf

URL: https://github.com/llvm/llvm-project/commit/5fbe9b958dc3035480406c2cd4524e4827d2dfaf
DIFF: https://github.com/llvm/llvm-project/commit/5fbe9b958dc3035480406c2cd4524e4827d2dfaf.diff

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

Added: 
    lld/test/COFF/cfguard-off-instrumented.s

Modified: 
    lld/COFF/Writer.cpp

Removed: 
    


################################################################################
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


        


More information about the llvm-commits mailing list