Skip to content

[LLD][COFF] Set __guard_flags to CF_INSTRUMENTED if any object is instrumented #115374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lld/COFF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions lld/test/COFF/cfguard-off-instrumented.s
Original file line number Diff line number Diff line change
@@ -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
Loading