tsan: support for linker initializer mutexes with static storage duration


git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@162021 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/tsan/rtl/tsan_rtl_mutex.cc b/lib/tsan/rtl/tsan_rtl_mutex.cc
index 9e7c78d..44c400b 100644
--- a/lib/tsan/rtl/tsan_rtl_mutex.cc
+++ b/lib/tsan/rtl/tsan_rtl_mutex.cc
@@ -24,10 +24,12 @@
   CHECK_GT(thr->in_rtl, 0);
   DPrintf("#%d: MutexCreate %zx\n", thr->tid, addr);
   StatInc(thr, StatMutexCreate);
-  MemoryWrite1Byte(thr, pc, addr);
+  if (!linker_init)
+    MemoryWrite1Byte(thr, pc, addr);
   SyncVar *s = ctx->synctab.GetAndLock(thr, pc, addr, true);
   s->is_rw = rw;
   s->is_recursive = recursive;
+  s->is_linker_init = linker_init;
   s->mtx.Unlock();
 }
 
@@ -36,16 +38,18 @@
   CHECK_GT(thr->in_rtl, 0);
   DPrintf("#%d: MutexDestroy %zx\n", thr->tid, addr);
   StatInc(thr, StatMutexDestroy);
-  MemoryWrite1Byte(thr, pc, addr);
   SyncVar *s = ctx->synctab.GetAndRemove(thr, pc, addr);
   if (s == 0)
     return;
-  if (s->owner_tid != SyncVar::kInvalidTid && !s->is_broken) {
-    s->is_broken = true;
-    ScopedReport rep(ReportTypeMutexDestroyLocked);
-    rep.AddMutex(s);
-    rep.AddLocation(s->addr, 1);
-    OutputReport(rep);
+  if (!s->is_linker_init) {
+    MemoryWrite1Byte(thr, pc, addr);
+    if (s->owner_tid != SyncVar::kInvalidTid && !s->is_broken) {
+      s->is_broken = true;
+      ScopedReport rep(ReportTypeMutexDestroyLocked);
+      rep.AddMutex(s);
+      rep.AddLocation(s->addr, 1);
+      OutputReport(rep);
+    }
   }
   DestroyAndFree(s);
 }
diff --git a/lib/tsan/rtl/tsan_sync.cc b/lib/tsan/rtl/tsan_sync.cc
index 2f8e785..2004c8e 100644
--- a/lib/tsan/rtl/tsan_sync.cc
+++ b/lib/tsan/rtl/tsan_sync.cc
@@ -24,7 +24,8 @@
   , recursion()
   , is_rw()
   , is_recursive()
-  , is_broken() {
+  , is_broken()
+  , is_linker_init() {
 }
 
 SyncTab::Part::Part()
diff --git a/lib/tsan/rtl/tsan_sync.h b/lib/tsan/rtl/tsan_sync.h
index 34d3e0b..c967130 100644
--- a/lib/tsan/rtl/tsan_sync.h
+++ b/lib/tsan/rtl/tsan_sync.h
@@ -64,6 +64,7 @@
   bool is_rw;
   bool is_recursive;
   bool is_broken;
+  bool is_linker_init;
   SyncVar *next;  // In SyncTab hashtable.
 
   uptr GetMemoryConsumption();