Mark constructor as static

Bug: http://b/183606176

Mark the constructor init_profile_extras as static so each library gets
its own copy of the constructor.  Also get rid of the
init_profile_extras_once flag which is unnecessary.

The same fix could be applied to profile-extras.cpp used for GCOV but
I'm skipping it since we don't officially support gcov and can be
cleaned up along with the rest of the build system support.

Test: verify that symbols from libjavacrypto.so (e.g. *NativeCrypto*)
are written from system_server

Change-Id: Ieedbeb609fd63963d76a067c2bc0291af7c04b1c
Merged-In: Ieedbeb609fd63963d76a067c2bc0291af7c04b1c
(cherry picked from commit 28ab69f68a07eb314017f09c7fb20cd07b4e40e9)
diff --git a/toolchain-extras/Android.bp b/toolchain-extras/Android.bp
index 007817e..220f3e3 100644
--- a/toolchain-extras/Android.bp
+++ b/toolchain-extras/Android.bp
@@ -129,11 +129,10 @@
     srcs: [
         "profile-clang-extras-test.cpp",
     ],
-    static_libs: [
+    whole_static_libs: [
         "libprofile-clang-extras",
     ],
     ldflags: [
-        "-uinit_profile_extras",
         "-Wl,--wrap,open",
     ],
     native_coverage: false,
diff --git a/toolchain-extras/profile-clang-extras.cpp b/toolchain-extras/profile-clang-extras.cpp
index 89c18b2..bb713e1 100644
--- a/toolchain-extras/profile-clang-extras.cpp
+++ b/toolchain-extras/profile-clang-extras.cpp
@@ -36,24 +36,15 @@
   }
 }
 
-__attribute__((weak)) int init_profile_extras_once = 0;
-
 // Initialize libprofile-extras:
-// - Install a signal handler that triggers __llvm_profile_write_file on <COVERAGE_FLUSH_SIGNAL>.
 //
-// We want this initializer to run during load time.
+// - Install a signal handler that triggers __llvm_profile_write_file on
+// <COVERAGE_FLUSH_SIGNAL>.
 //
-// Just marking init_profile_extras() with __attribute__((constructor)) isn't
-// enough since the linker drops it from its output since no other symbol from
-// this static library is referenced.
-//
-// We force the linker to include init_profile_extras() by passing
-// '-uinit_profile_extras' to the linker (in build/soong).
-__attribute__((constructor)) int init_profile_extras(void) {
-  if (init_profile_extras_once)
-    return 0;
-  init_profile_extras_once = 1;
-
+// We want this initializer to run during load time.  In addition to marking
+// this function as a constructor, we link this library with `--whole-archive`
+// to force this function to be included in the output.
+static __attribute__((constructor)) int init_profile_extras(void) {
   if (chained_signal_handler != SIG_ERR) {
     return -1;
   }