Add secondary counter in place of tls_nodelete flag

The tls_nodelete state should apply to load_group not
isolated soinfo. This actually also means that multiple
soinfos may have tls_counter on their dso_handles.

This change replaces TLS_NODELETE flag with secondary counter.
Note that access to the secondary counter (located inside soinfo)
is pretty expensive because it requires soinfo lookup by dso_handle
whereas dso_handle counter is much faster. This is why it is updated
only when dso_handle counter starts or hits 0.

Bug: http://b/80278285
Test: bionic-unit-tests --gtest_filter=dl*
Change-Id: I535583f6714e45fa2a7eaf7bb3126da20ee7cba9
diff --git a/tests/libs/Android.bp b/tests/libs/Android.bp
index c1f5f1c..2d35c51 100644
--- a/tests/libs/Android.bp
+++ b/tests/libs/Android.bp
@@ -700,7 +700,7 @@
 }
 
 // -----------------------------------------------------------------------------
-// Library with non-trivial thread_local variable to test dlclose()
+// Libraries with non-trivial thread_local variable to test dlclose()
 // -----------------------------------------------------------------------------
 cc_test_library {
     name: "libtest_thread_local_dtor",
@@ -708,6 +708,26 @@
     srcs: ["thread_local_dtor.cpp"],
 }
 
+cc_test_library {
+    name: "libtest_thread_local_dtor2",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["thread_local_dtor2.cpp"],
+}
+
+// -----------------------------------------------------------------------------
+// Library dt_needs libtest_thread_local_dtor/2 (to check no-unload on load_group)
+// -----------------------------------------------------------------------------
+cc_test_library {
+    name: "libtest_indirect_thread_local_dtor",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["empty.cpp"],
+    shared_libs: [
+        "libtest_thread_local_dtor",
+        "libtest_thread_local_dtor2",
+    ],
+}
+
+
 // -----------------------------------------------------------------------------
 // Tool to use to align the shared libraries in a zip file.
 // -----------------------------------------------------------------------------
diff --git a/tests/libs/thread_local_dtor2.cpp b/tests/libs/thread_local_dtor2.cpp
new file mode 100644
index 0000000..9b2b164
--- /dev/null
+++ b/tests/libs/thread_local_dtor2.cpp
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+namespace {
+
+class TestClass {
+ public:
+  TestClass(bool* flag) : flag_(flag) {}
+  ~TestClass() {
+    *flag_ = true;
+  }
+ private:
+  bool* flag_;
+};
+
+};  // namespace
+
+extern "C" void init_thread_local_variable2(bool* flag) {
+  thread_local TestClass test(flag);
+}