Skip to content

Commit 369ac05

Browse files
committed
Clean up adding type metadata for potential indirect targets.
1 parent 4bedac1 commit 369ac05

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,17 +2848,13 @@ 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-
createFunctionTypeMetadataForIcallForCFI(FD, F);
2851+
createFunctionTypeMetadataForIcall(FD, F);
28522852
}
28532853
}
28542854

28552855
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);
2861-
}
2856+
if (auto *FD = dyn_cast<FunctionDecl>(D))
2857+
createIndirectFunctionTypeMD(FD, F);
28622858
}
28632859

28642860
// Emit type metadata on member functions for member function pointer checks.
@@ -3069,22 +3065,22 @@ static bool hasExistingGeneralizedTypeMD(llvm::Function *F) {
30693065
return MD && MD->hasGeneralizedMDString();
30703066
}
30713067

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)
3068+
void CodeGenModule::createIndirectFunctionTypeMD(const FunctionDecl *FD,
3069+
llvm::Function *F) {
3070+
// Return if generalized type metadata is already attached.
3071+
if (hasExistingGeneralizedTypeMD(F))
30763072
return;
3077-
3078-
if (!hasExistingGeneralizedTypeMD(F) &&
3079-
(!F->hasLocalLinkage() ||
3080-
F->getFunction().hasAddressTaken(nullptr, /*IgnoreCallbackUses=*/true,
3081-
/*IgnoreAssumeLikeCalls=*/true,
3082-
/*IgnoreLLVMUsed=*/false)))
3073+
// All functions which are not internal linkage could be indirect targets.
3074+
// Address taken functions with internal linkage could be indirect targets.
3075+
if (!F->hasLocalLinkage() &&
3076+
F->getFunction().hasAddressTaken(nullptr, /*IgnoreCallbackUses=*/true,
3077+
/*IgnoreAssumeLikeCalls=*/true,
3078+
/*IgnoreLLVMUsed=*/false))
30833079
F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType()));
30843080
}
30853081

3086-
void CodeGenModule::createFunctionTypeMetadataForIcallForCFI(
3087-
const FunctionDecl *FD, llvm::Function *F) {
3082+
void CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
3083+
llvm::Function *F) {
30883084

30893085
// Only if we are checking indirect calls.
30903086
if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall))
@@ -3260,7 +3256,7 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
32603256
// jump table.
32613257
if (!CodeGenOpts.SanitizeCfiCrossDso ||
32623258
!CodeGenOpts.SanitizeCfiCanonicalJumpTables)
3263-
createFunctionTypeMetadataForIcallForCFI(FD, F);
3259+
createFunctionTypeMetadataForIcall(FD, F);
32643260

32653261
if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
32663262
setKCFIType(FD, F);

clang/lib/CodeGen/CodeGenModule.h

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

16431643
/// Create and attach type metadata to the given function.
1644-
void createFunctionTypeMetadataForIcallForCFI(const FunctionDecl *FD,
1645-
llvm::Function *F);
1644+
void createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
1645+
llvm::Function *F);
16461646

16471647
/// Create and attach type metadata if the function is a potential indirect
16481648
/// call target to support call graph section.
1649-
void createFunctionTypeMDForIcallForCallGraph(const FunctionDecl *FD,
1650-
llvm::Function *F);
1649+
void createIndirectFunctionTypeMD(const FunctionDecl *FD, llvm::Function *F);
16511650

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

0 commit comments

Comments
 (0)