summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms
diff options
context:
space:
mode:
authorMatt Morehouse <mascasa@google.com>2018-09-11 15:23:14 +0000
committerMatt Morehouse <mascasa@google.com>2018-09-11 15:23:14 +0000
commiteac270caf45ce957daf3c01b01a5752a818344bd (patch)
tree8b5f5ac01d219822e55b3a2fa825dde2030b16c8 /llvm/lib/Transforms
parent873cae5ae7efdbe9fdfe7b7ffba923516b7d14af (diff)
downloadbcm5719-llvm-eac270caf45ce957daf3c01b01a5752a818344bd.tar.gz
bcm5719-llvm-eac270caf45ce957daf3c01b01a5752a818344bd.zip
[SanitizerCoverage] Create comdat for global arrays.
Summary: Place global arrays in comdat sections with their associated functions. This makes sure they are stripped along with the functions they reference, even on the BFD linker. Reviewers: eugenis Reviewed By: eugenis Subscribers: eraman, hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D51902 llvm-svn: 341951
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r--llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp45
1 files changed, 31 insertions, 14 deletions
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
index 1f97e909851..11d08d4d867 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
@@ -219,6 +219,11 @@ private:
MDNode::get(*C, None));
}
+ std::string UniqueSuffix() {
+ static size_t Count = 0;
+ return "_sancov" + std::to_string(Count++);
+ }
+
std::string getSectionName(const std::string &Section) const;
std::string getSectionStart(const std::string &Section) const;
std::string getSectionEnd(const std::string &Section) const;
@@ -571,11 +576,34 @@ GlobalVariable *SanitizerCoverageModule::CreateFunctionLocalArrayInSection(
auto Array = new GlobalVariable(
*CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage,
Constant::getNullValue(ArrayTy), "__sancov_gen_");
- if (auto Comdat = F.getComdat())
+ if (auto Comdat = F.getComdat()) {
+ Array->setComdat(Comdat);
+ } else {
+ // TODO: Refactor into a helper function and use it in ASan.
+ assert(F.hasName());
+ std::string Name = F.getName();
+ if (F.hasLocalLinkage()) {
+ std::string ModuleId = getUniqueModuleId(CurModule);
+ Name += ModuleId.empty() ? UniqueSuffix() : ModuleId;
+ }
+ Comdat = CurModule->getOrInsertComdat(Name);
+ // Make this IMAGE_COMDAT_SELECT_NODUPLICATES on COFF. Also upgrade private
+ // linkage to internal linkage so that a symbol table entry is emitted. This
+ // is necessary in order to create the comdat group.
+ if (TargetTriple.isOSBinFormatCOFF()) {
+ Comdat->setSelectionKind(Comdat::NoDuplicates);
+ if (F.hasPrivateLinkage())
+ F.setLinkage(GlobalValue::InternalLinkage);
+ }
+ F.setComdat(Comdat);
Array->setComdat(Comdat);
+ }
Array->setSection(getSectionName(Section));
Array->setAlignment(Ty->isPointerTy() ? DL->getPointerSize()
: Ty->getPrimitiveSizeInBits() / 8);
+ GlobalsToAppendToCompilerUsed.push_back(Array);
+ MDNode *MD = MDNode::get(F.getContext(), ValueAsMetadata::get(&F));
+ Array->addMetadata(LLVMContext::MD_associated, *MD);
return Array;
}
@@ -613,23 +641,12 @@ void SanitizerCoverageModule::CreateFunctionLocalArrays(
FunctionGuardArray = CreateFunctionLocalArrayInSection(
AllBlocks.size(), F, Int32Ty, SanCovGuardsSectionName);
GlobalsToAppendToUsed.push_back(FunctionGuardArray);
- GlobalsToAppendToCompilerUsed.push_back(FunctionGuardArray);
- MDNode *MD = MDNode::get(F.getContext(), ValueAsMetadata::get(&F));
- FunctionGuardArray->addMetadata(LLVMContext::MD_associated, *MD);
}
- if (Options.Inline8bitCounters) {
+ if (Options.Inline8bitCounters)
Function8bitCounterArray = CreateFunctionLocalArrayInSection(
AllBlocks.size(), F, Int8Ty, SanCovCountersSectionName);
- GlobalsToAppendToCompilerUsed.push_back(Function8bitCounterArray);
- MDNode *MD = MDNode::get(F.getContext(), ValueAsMetadata::get(&F));
- Function8bitCounterArray->addMetadata(LLVMContext::MD_associated, *MD);
- }
- if (Options.PCTable) {
+ if (Options.PCTable)
FunctionPCsArray = CreatePCArray(F, AllBlocks);
- GlobalsToAppendToCompilerUsed.push_back(FunctionPCsArray);
- MDNode *MD = MDNode::get(F.getContext(), ValueAsMetadata::get(&F));
- FunctionPCsArray->addMetadata(LLVMContext::MD_associated, *MD);
- }
}
bool SanitizerCoverageModule::InjectCoverage(Function &F,
OpenPOWER on IntegriCloud