diff options
| author | Matt Morehouse <mascasa@google.com> | 2018-09-13 21:45:55 +0000 |
|---|---|---|
| committer | Matt Morehouse <mascasa@google.com> | 2018-09-13 21:45:55 +0000 |
| commit | 3bea25e5543606758ab10d395b1e7283255be75b (patch) | |
| tree | 17e7690ff7da19c1e9aa1a3ce69ad7fc1ac3309a /llvm/lib/Transforms | |
| parent | 3815e702e751efb14188c0f0beeb10e343d8f053 (diff) | |
| download | bcm5719-llvm-3bea25e5543606758ab10d395b1e7283255be75b.tar.gz bcm5719-llvm-3bea25e5543606758ab10d395b1e7283255be75b.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: 342186
Diffstat (limited to 'llvm/lib/Transforms')
| -rw-r--r-- | llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp index 1f97e909851..2a055920c3e 100644 --- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp +++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp @@ -219,6 +219,8 @@ private: MDNode::get(*C, None)); } + Comdat *GetOrCreateFunctionComdat(Function &F); + std::string getSectionName(const std::string &Section) const; std::string getSectionStart(const std::string &Section) const; std::string getSectionEnd(const std::string &Section) const; @@ -234,6 +236,7 @@ private: Type *IntptrTy, *IntptrPtrTy, *Int64Ty, *Int64PtrTy, *Int32Ty, *Int32PtrTy, *Int16Ty, *Int8Ty, *Int8PtrTy; Module *CurModule; + std::string CurModuleUniqueId; Triple TargetTriple; LLVMContext *C; const DataLayout *DL; @@ -304,6 +307,7 @@ bool SanitizerCoverageModule::runOnModule(Module &M) { C = &(M.getContext()); DL = &M.getDataLayout(); CurModule = &M; + CurModuleUniqueId = getUniqueModuleId(CurModule); TargetTriple = Triple(M.getTargetTriple()); FunctionGuardArray = nullptr; Function8bitCounterArray = nullptr; @@ -565,17 +569,35 @@ bool SanitizerCoverageModule::runOnFunction(Function &F) { return true; } +Comdat *SanitizerCoverageModule::GetOrCreateFunctionComdat(Function &F) { + if (auto Comdat = F.getComdat()) return Comdat; + if (!TargetTriple.isOSBinFormatELF()) return nullptr; + assert(F.hasName()); + std::string Name = F.getName(); + if (F.hasLocalLinkage()) { + if (CurModuleUniqueId.empty()) return nullptr; + Name += CurModuleUniqueId; + } + auto Comdat = CurModule->getOrInsertComdat(Name); + F.setComdat(Comdat); + return Comdat; +} + GlobalVariable *SanitizerCoverageModule::CreateFunctionLocalArrayInSection( size_t NumElements, Function &F, Type *Ty, const char *Section) { ArrayType *ArrayTy = ArrayType::get(Ty, NumElements); auto Array = new GlobalVariable( *CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage, Constant::getNullValue(ArrayTy), "__sancov_gen_"); - if (auto Comdat = F.getComdat()) + if (auto Comdat = GetOrCreateFunctionComdat(F)) 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 +635,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, |

