Skip to content

Commit 4bedac1

Browse files
committed
Refactor call graph type metadata addition code out of CFI flow.
1 parent dd76511 commit 4bedac1

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,7 +2848,16 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
28482848
// Skip available_externally functions. They won't be codegen'ed in the
28492849
// current module anyway.
28502850
if (getContext().GetGVALinkageForFunction(FD) != GVA_AvailableExternally)
2851-
createFunctionTypeMetadataForIcall(FD, F);
2851+
createFunctionTypeMetadataForIcallForCFI(FD, F);
2852+
}
2853+
}
2854+
2855+
if (CodeGenOpts.CallGraphSection) {
2856+
if (auto *FD = dyn_cast<FunctionDecl>(D)) {
2857+
// Skip available_externally functions. They won't be codegen'ed in the
2858+
// current module anyway.
2859+
if (getContext().GetGVALinkageForFunction(FD) != GVA_AvailableExternally)
2860+
createFunctionTypeMDForIcallForCallGraph(FD, F);
28522861
}
28532862
}
28542863

@@ -3060,16 +3069,24 @@ static bool hasExistingGeneralizedTypeMD(llvm::Function *F) {
30603069
return MD && MD->hasGeneralizedMDString();
30613070
}
30623071

3063-
void CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
3064-
llvm::Function *F) {
3065-
if (CodeGenOpts.CallGraphSection && !hasExistingGeneralizedTypeMD(F) &&
3072+
void CodeGenModule::createFunctionTypeMDForIcallForCallGraph(
3073+
const FunctionDecl *FD, llvm::Function *F) {
3074+
// Add additional metadata if we are emitting call graph section
3075+
if (!CodeGenOpts.CallGraphSection)
3076+
return;
3077+
3078+
if (!hasExistingGeneralizedTypeMD(F) &&
30663079
(!F->hasLocalLinkage() ||
30673080
F->getFunction().hasAddressTaken(nullptr, /*IgnoreCallbackUses=*/true,
30683081
/*IgnoreAssumeLikeCalls=*/true,
30693082
/*IgnoreLLVMUsed=*/false)))
30703083
F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType()));
3084+
}
3085+
3086+
void CodeGenModule::createFunctionTypeMetadataForIcallForCFI(
3087+
const FunctionDecl *FD, llvm::Function *F) {
30713088

3072-
// Add additional metadata only if we are checking indirect calls with CFI.
3089+
// Only if we are checking indirect calls.
30733090
if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall))
30743091
return;
30753092

@@ -3243,7 +3260,7 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
32433260
// jump table.
32443261
if (!CodeGenOpts.SanitizeCfiCrossDso ||
32453262
!CodeGenOpts.SanitizeCfiCanonicalJumpTables)
3246-
createFunctionTypeMetadataForIcall(FD, F);
3263+
createFunctionTypeMetadataForIcallForCFI(FD, F);
32473264

32483265
if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
32493266
setKCFIType(FD, F);

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,8 +1641,13 @@ class CodeGenModule : public CodeGenTypeCache {
16411641
llvm::Metadata *CreateMetadataIdentifierGeneralized(QualType T);
16421642

16431643
/// Create and attach type metadata to the given function.
1644-
void createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
1645-
llvm::Function *F);
1644+
void createFunctionTypeMetadataForIcallForCFI(const FunctionDecl *FD,
1645+
llvm::Function *F);
1646+
1647+
/// Create and attach type metadata if the function is a potential indirect
1648+
/// call target to support call graph section.
1649+
void createFunctionTypeMDForIcallForCallGraph(const FunctionDecl *FD,
1650+
llvm::Function *F);
16461651

16471652
/// Create and attach type metadata to the given call.
16481653
void createCalleeTypeMetadataForIcall(const QualType &QT, llvm::CallBase *CB);

0 commit comments

Comments
 (0)