Skip to content

Commit 5fbe9b9

Browse files
authoredNov 8, 2024··
[LLD][COFF] Set __guard_flags to CF_INSTRUMENTED if any object is instrumented (#115374)
1 parent afa178d commit 5fbe9b9

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed
 

‎lld/COFF/Writer.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,7 @@ void Writer::createMiscChunks() {
12171217
createSEHTable();
12181218

12191219
// Create /guard:cf tables if requested.
1220-
if (config->guardCF != GuardCFLevel::Off)
1221-
createGuardCFTables();
1220+
createGuardCFTables();
12221221

12231222
if (isArm64EC(config->machine))
12241223
createECChunks();
@@ -1979,6 +1978,20 @@ void Writer::markSymbolsWithRelocations(ObjFile *file,
19791978
void Writer::createGuardCFTables() {
19801979
Configuration *config = &ctx.config;
19811980

1981+
if (config->guardCF == GuardCFLevel::Off) {
1982+
// MSVC marks the entire image as instrumented if any input object was built
1983+
// with /guard:cf.
1984+
for (ObjFile *file : ctx.objFileInstances) {
1985+
if (file->hasGuardCF()) {
1986+
Symbol *flagSym = ctx.symtab.findUnderscore("__guard_flags");
1987+
cast<DefinedAbsolute>(flagSym)->setVA(
1988+
uint32_t(GuardFlags::CF_INSTRUMENTED));
1989+
break;
1990+
}
1991+
}
1992+
return;
1993+
}
1994+
19821995
SymbolRVASet addressTakenSyms;
19831996
SymbolRVASet giatsRVASet;
19841997
std::vector<Symbol *> giatsSymbols;
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Verify that __guard_flags is set to CF_INSTRUMENTED if CF guard is disabled,
2+
// but the input object was built with CF guard.
3+
4+
// REQUIRES: x86
5+
6+
// RUN: llvm-mc -filetype=obj -triple=x86_64-windows %s -o %t.obj
7+
// RUN: lld-link -out:%t1.dll %t.obj -dll -noentry
8+
// RUN: lld-link -out:%t2.dll %t.obj -dll -noentry -guard:no
9+
10+
// RUN: llvm-readobj --hex-dump=.test %t1.dll | FileCheck %s
11+
// RUN: llvm-readobj --hex-dump=.test %t2.dll | FileCheck %s
12+
// CHECK: 0x180001000 00010000
13+
14+
.def @feat.00;
15+
.scl 3;
16+
.type 0;
17+
.endef
18+
.globl @feat.00
19+
@feat.00 = 0x800
20+
21+
.section .test, "r"
22+
.long __guard_flags

0 commit comments

Comments
 (0)
Please sign in to comment.