[SanitizerCoverage] Use External on Windows
authorFangrui Song <i@maskray.me>
Tue, 23 Mar 2021 06:05:36 +0000 (23:05 -0700)
committerFangrui Song <i@maskray.me>
Tue, 23 Mar 2021 06:05:36 +0000 (23:05 -0700)
This should fix https://reviews.llvm.org/D98903#2643589 though
it is not clear to me why ExternalWeak does not work.

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
llvm/test/Instrumentation/SanitizerCoverage/coff-pc-table-inline-8bit-counters.ll
llvm/test/Instrumentation/SanitizerCoverage/coff-pc-table-inline-bool-flag.ll

index 16ba84f..166dd10 100644 (file)
@@ -330,13 +330,18 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
                                            Type *Ty) {
   // Use ExternalWeak so that if all sections are discarded due to section
   // garbage collection, the linker will not report undefined symbol errors.
-  GlobalVariable *SecStart = new GlobalVariable(
-      M, Ty->getPointerElementType(), false,
-      GlobalVariable::ExternalWeakLinkage, nullptr, getSectionStart(Section));
+  // Windows defines the start/stop symbols in compiler-rt so no need for
+  // ExternalWeak.
+  GlobalValue::LinkageTypes Linkage = TargetTriple.isOSBinFormatCOFF()
+                                          ? GlobalVariable::ExternalLinkage
+                                          : GlobalVariable::ExternalWeakLinkage;
+  GlobalVariable *SecStart =
+      new GlobalVariable(M, Ty->getPointerElementType(), false, Linkage,
+                         nullptr, getSectionStart(Section));
   SecStart->setVisibility(GlobalValue::HiddenVisibility);
-  GlobalVariable *SecEnd = new GlobalVariable(
-      M, Ty->getPointerElementType(), false,
-      GlobalVariable::ExternalWeakLinkage, nullptr, getSectionEnd(Section));
+  GlobalVariable *SecEnd =
+      new GlobalVariable(M, Ty->getPointerElementType(), false, Linkage,
+                         nullptr, getSectionEnd(Section));
   SecEnd->setVisibility(GlobalValue::HiddenVisibility);
   IRBuilder<> IRB(M.getContext());
   if (!TargetTriple.isOSBinFormatCOFF())
index 0134c7e..445a96d 100644 (file)
@@ -11,3 +11,5 @@ entry:
 
 ; CHECK-DAG: section ".SCOV{{\$}}CM",
 ; CHECK-DAG: section ".SCOVP{{\$}}M",
+; CHECK:     @__start___sancov_cntrs = external hidden global i8
+; CHECK:     @__stop___sancov_cntrs = external hidden global i8
index d91714b..2f046f9 100644 (file)
@@ -11,3 +11,5 @@ entry:
 
 ; CHECK-DAG: section ".SCOV{{\$}}BM",
 ; CHECK-DAG: section ".SCOVP{{\$}}M",
+; CHECK:     @__start___sancov_bools = external hidden global i1
+; CHECK:     @__stop___sancov_bools = external hidden global i1