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

This commit is contained in:
Jacek Caban
2024-11-08 14:01:25 +01:00
committed by GitHub
parent afa178d360
commit 5fbe9b958d
2 changed files with 37 additions and 2 deletions

View File

@@ -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;

View File

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