diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp index 71ee5ce468555..58d0700c52aaf 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(flagSym)->setVA( + uint32_t(GuardFlags::CF_INSTRUMENTED)); + break; + } + } + return; + } + SymbolRVASet addressTakenSyms; SymbolRVASet giatsRVASet; std::vector giatsSymbols; diff --git a/lld/test/COFF/cfguard-off-instrumented.s b/lld/test/COFF/cfguard-off-instrumented.s new file mode 100644 index 0000000000000..4bd81d9956892 --- /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 +@feat.00 = 0x800 + + .section .test, "r" + .long __guard_flags