diff options
author | Justin Bogner <mail@justinbogner.com> | 2017-02-01 02:38:39 +0000 |
---|---|---|
committer | Justin Bogner <mail@justinbogner.com> | 2017-02-01 02:38:39 +0000 |
commit | 41e632bf6b45f843248f0df8b87af941b22203bc (patch) | |
tree | e9df590c5288e23f4d68fcb2e624ddb7f2307916 /llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp | |
parent | 27dcc6c0e2a7ca244f3acaef327d3f244ccadfce (diff) | |
download | bcm5719-llvm-41e632bf6b45f843248f0df8b87af941b22203bc.tar.gz bcm5719-llvm-41e632bf6b45f843248f0df8b87af941b22203bc.zip |
SanitizerCoverage: Support sanitizer guard section on darwin
MachO's sections need a segment as well as a section name, and the
section start and end symbols are spelled differently than on ELF.
llvm-svn: 293733
Diffstat (limited to 'llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp')
-rw-r--r-- | llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp index e34dd611b25..b6ec70a413e 100644 --- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp +++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp @@ -78,7 +78,6 @@ static const char *const SanCovTraceSwitchName = "__sanitizer_cov_trace_switch"; static const char *const SanCovModuleCtorName = "sancov.module_ctor"; static const uint64_t SanCtorAndDtorPriority = 2; -static const char *const SanCovTracePCGuardSection = "__sancov_guards"; static const char *const SanCovTracePCGuardName = "__sanitizer_cov_trace_pc_guard"; static const char *const SanCovTracePCGuardInitName = @@ -139,6 +138,24 @@ static cl::opt<bool> ClUse8bitCounters("sanitizer-coverage-8bit-counters", cl::desc("Experimental 8-bit counters"), cl::Hidden, cl::init(false)); +static StringRef getSanCovTracePCGuardSection(const Module &M) { + return Triple(M.getTargetTriple()).isOSBinFormatMachO() + ? "__DATA,__sancov_guards" + : "__sancov_guards"; +} + +static StringRef getSanCovTracePCGuardSectionStart(const Module &M) { + return Triple(M.getTargetTriple()).isOSBinFormatMachO() + ? "\1section$start$__DATA$__sancov_guards" + : "__start___sancov_guards"; +} + +static StringRef getSanCovTracePCGuardSectionEnd(const Module &M) { + return Triple(M.getTargetTriple()).isOSBinFormatMachO() + ? "\1section$end$__DATA$__sancov_guards" + : "__stop___sancov_guards"; +} + namespace { SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) { @@ -363,20 +380,20 @@ bool SanitizerCoverageModule::runOnModule(Module &M) { if (Options.TracePCGuard) { if (HasSancovGuardsSection) { Function *CtorFunc; - std::string SectionName(SanCovTracePCGuardSection); - GlobalVariable *Bounds[2]; - const char *Prefix[2] = {"__start_", "__stop_"}; - for (int i = 0; i < 2; i++) { - Bounds[i] = new GlobalVariable(M, Int32PtrTy, false, - GlobalVariable::ExternalLinkage, nullptr, - Prefix[i] + SectionName); - Bounds[i]->setVisibility(GlobalValue::HiddenVisibility); - } + GlobalVariable *SecStart = new GlobalVariable( + M, Int32PtrTy, false, GlobalVariable::ExternalLinkage, nullptr, + getSanCovTracePCGuardSectionStart(*CurModule)); + SecStart->setVisibility(GlobalValue::HiddenVisibility); + GlobalVariable *SecEnd = new GlobalVariable( + M, Int32PtrTy, false, GlobalVariable::ExternalLinkage, nullptr, + getSanCovTracePCGuardSectionEnd(*CurModule)); + SecEnd->setVisibility(GlobalValue::HiddenVisibility); + std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions( M, SanCovModuleCtorName, SanCovTracePCGuardInitName, {Int32PtrTy, Int32PtrTy}, - {IRB.CreatePointerCast(Bounds[0], Int32PtrTy), - IRB.CreatePointerCast(Bounds[1], Int32PtrTy)}); + {IRB.CreatePointerCast(SecStart, Int32PtrTy), + IRB.CreatePointerCast(SecEnd, Int32PtrTy)}); appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority); } @@ -517,7 +534,7 @@ void SanitizerCoverageModule::CreateFunctionGuardArray(size_t NumGuards, Constant::getNullValue(ArrayOfInt32Ty), "__sancov_gen_"); if (auto Comdat = F.getComdat()) FunctionGuardArray->setComdat(Comdat); - FunctionGuardArray->setSection(SanCovTracePCGuardSection); + FunctionGuardArray->setSection(getSanCovTracePCGuardSection(*CurModule)); } bool SanitizerCoverageModule::InjectCoverage(Function &F, |