Link all internal libraries statically into dex2oat(d) on host.

All static defaults had to change to whole_static_libs to work with the
new libdex2oat(d)_static libraries, so that they don't lose all symbols
in the static_libs dependency from the dex2oat(d) binaries.

The static unstripped dex2oat binary is 348 MB, 35 MB smaller than the
sum of the dynamic binary + DSO's.

Test: art/build/apex/runtests.sh
Test: runtests in device chroot
Test: art/test/testrunner/run_build_test_target.py art-test
Test: readelf -d out/host/linux-x86/bin/dex2oat(d)
  Check that NEEDED only lists system libraries (libdl, libpthread,
  libm, librt, libgcc_s, libc).
Bug: 145934348
Change-Id: I7abcf1db9b053f71ced6a7dde38ab1b4300b7b5a
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk
index a973c36..7333bba 100644
--- a/build/Android.gtest.mk
+++ b/build/Android.gtest.mk
@@ -26,7 +26,7 @@
 # Manually add system libraries that we need to run the host ART tools.
 my_files += \
   $(foreach lib, libbacktrace libbase libc++ libicu_jni liblog libsigchain libunwindstack \
-    libziparchive libjavacore libandroidio libopenjdkd liblz4, \
+    libziparchive libjavacore libandroidio libopenjdkd, \
     $(call intermediates-dir-for,SHARED_LIBRARIES,$(lib),HOST)/$(lib).so:lib64/$(lib).so \
     $(call intermediates-dir-for,SHARED_LIBRARIES,$(lib),HOST,,2ND)/$(lib).so:lib/$(lib).so) \
   $(foreach lib, libcrypto libz libicuuc libicui18n libandroidicu libexpat, \
diff --git a/compiler/Android.bp b/compiler/Android.bp
index a0118ca..332754e 100644
--- a/compiler/Android.bp
+++ b/compiler/Android.bp
@@ -173,7 +173,7 @@
 
 cc_defaults {
     name: "libart-compiler_static_base_defaults",
-    static_libs: [
+    whole_static_libs: [
         "libbase",
     ],
 }
@@ -271,7 +271,7 @@
         "libdexfile_static_defaults",
         "libprofile_static_defaults",
     ],
-    static_libs: ["libart-compiler"],
+    whole_static_libs: ["libart-compiler"],
 }
 
 art_cc_library {
@@ -344,7 +344,7 @@
         "libdexfiled_static_defaults",
         "libprofiled_static_defaults",
     ],
-    static_libs: ["libartd-compiler"],
+    whole_static_libs: ["libartd-compiler"],
 }
 
 art_cc_library {
diff --git a/dex2oat/Android.bp b/dex2oat/Android.bp
index ef3ebb2..8d2f8e2 100644
--- a/dex2oat/Android.bp
+++ b/dex2oat/Android.bp
@@ -67,7 +67,7 @@
 
 cc_defaults {
     name: "libart-dex2oat_static_base_defaults",
-    static_libs: [
+    whole_static_libs: [
         "libbase",
         "libcrypto",
         "liblz4",
@@ -109,13 +109,30 @@
         "libart_static_defaults",
         "libprofile_static_defaults",
     ],
-    static_libs: [
+    whole_static_libs: [
         "libart-compiler",
         "libart-dexlayout",
         "libart-dex2oat",
     ],
 }
 
+// Collect all the static defaults and build a host-only static library, which
+// is then used for the (mostly) static host dex2oat binary.
+art_cc_library_static {
+    name: "libdex2oat_static",
+    device_supported: false,
+    host_supported: true,
+    defaults: [
+        "libart-compiler_static_defaults",
+        "libart-dex2oat_static_defaults",
+        "libart-dexlayout_static_defaults",
+        "libart_static_defaults",
+        "libartbase_static_defaults",
+        "libdexfile_static_defaults",
+        "libprofile_static_defaults",
+    ],
+}
+
 art_cc_library_static {
     name: "libartd-dex2oat",
     defaults: [
@@ -141,30 +158,54 @@
         "libartd_static_defaults",
         "libprofiled_static_defaults",
     ],
-    static_libs: [
+    whole_static_libs: [
         "libartd-compiler",
         "libartd-dexlayout",
         "libartd-dex2oat",
     ],
 }
 
+art_cc_library_static {
+    name: "libdex2oatd_static",
+    device_supported: false,
+    host_supported: true,
+    defaults: [
+        "libartbased_static_defaults",
+        "libartd-compiler_static_defaults",
+        "libartd-dex2oat_static_defaults",
+        "libartd-dexlayout_static_defaults",
+        "libartd_static_defaults",
+        "libdexfiled_static_defaults",
+        "libprofiled_static_defaults",
+    ],
+}
+
 cc_defaults {
     name: "dex2oat-defaults",
     host_supported: true,
     defaults: ["art_defaults"],
+
     srcs: [
         "dex2oat_options.cc",
         "dex2oat.cc",
     ],
+    header_libs: [
+        "art_cmdlineparser_headers",
+    ],
 
     target: {
         android: {
             compile_multilib: "both",
+            shared_libs: [
+                "libartpalette",
+                "libbase",
+                "libcrypto",
+                "liblz4", // libart(d)-dex2oat dependency; must be repeated here since it's a static lib.
+                "libsigchain",
+                "libz",
+            ],
         },
     },
-    header_libs: [
-        "art_cmdlineparser_headers",
-    ],
 }
 
 cc_defaults {
@@ -210,23 +251,6 @@
     // Modules that do dexpreopting, e.g. android_app, depend implicitly on
     // either dex2oat or dex2oatd in ART source builds.
     visibility: ["//visibility:public"],
-    shared_libs: [
-        "libart",
-        "libart-compiler",
-        "libart-dexlayout",
-        "libartbase",
-        "libartpalette",
-        "libbase",
-        "libcrypto",
-        "libdexfile",
-        "liblz4", // libart-dex2oat dependency; must be repeated here since it's a static lib.
-        "libprofile",
-        "libsigchain",
-        "libz",
-    ],
-    static_libs: [
-        "libart-dex2oat",
-    ],
 
     multilib: {
         lib32: {
@@ -247,11 +271,26 @@
     },
     target: {
         android: {
+            shared_libs: [
+                "libart",
+                "libart-compiler",
+                "libart-dexlayout",
+                "libartbase",
+                "libdexfile",
+                "libprofile",
+            ],
+            static_libs: [
+                "libart-dex2oat",
+            ],
             lto: {
                 thin: true,
             },
         },
         host: {
+            // Make the host binary static, except for system libraries. This
+            // avoids having to bundle host dynamic libs in prebuilts.
+            static_libs: ["libdex2oat_static"],
+            stl: "c++_static",
             // Override the prefer32 added by art_cc_binary when
             // HOST_PREFER_32_BIT is in use. Necessary because the logic in
             // Soong for setting ctx.Config().BuildOSTarget (used in
@@ -278,25 +317,25 @@
     // Modules that do dexpreopting, e.g. android_app, depend implicitly on
     // either dex2oat or dex2oatd in ART source builds.
     visibility: ["//visibility:public"],
-    shared_libs: [
-        "libartbased",
-        "libartd",
-        "libartd-compiler",
-        "libartd-dexlayout",
-        "libartpalette",
-        "libbase",
-        "libcrypto",
-        "libdexfiled",
-        "liblz4", // libartd-dex2oat dependency; must be repeated here since it's a static lib.
-        "libprofiled",
-        "libsigchain",
-        "libz",
-    ],
-    static_libs: [
-        "libartd-dex2oat",
-    ],
     target: {
+        android: {
+            shared_libs: [
+                "libartbased",
+                "libartd",
+                "libartd-compiler",
+                "libartd-dexlayout",
+                "libdexfiled",
+                "libprofiled",
+            ],
+            static_libs: [
+                "libartd-dex2oat",
+            ],
+        },
         host: {
+            // Make the host binary static, except for system libraries. This
+            // avoids having to bundle host dynamic libs in prebuilts.
+            static_libs: ["libdex2oatd_static"],
+            stl: "c++_static",
             // Override the prefer32 added by art_cc_binary when
             // HOST_PREFER_32_BIT is in use. Necessary because the logic in
             // Soong for setting ctx.Config().BuildOSTarget (used in
@@ -342,25 +381,12 @@
         // Try to get rid of it.
         "-z muldefs",
     ],
-    static_libs: [
-        "libbase",
-        "libsigchain_fake",
-        "libz",
-    ],
 }
 
 art_cc_binary {
     name: "dex2oats",
-    defaults: [
-        "dex2oats-defaults",
-        "libart_static_defaults",
-        "libart-compiler_static_defaults",
-        "libart-dexlayout_static_defaults",
-        "libartbase_static_defaults",
-        "libdexfile_static_defaults",
-        "libprofile_static_defaults",
-        "libart-dex2oat_static_defaults",
-    ],
+    defaults: ["dex2oats-defaults"],
+    static_libs: ["libdex2oat_static"],
 }
 
 art_cc_binary {
@@ -368,14 +394,8 @@
     defaults: [
         "art_debug_defaults",
         "dex2oats-defaults",
-        "libartd_static_defaults",
-        "libartd-compiler_static_defaults",
-        "libartd-dexlayout_static_defaults",
-        "libartbased_static_defaults",
-        "libdexfiled_static_defaults",
-        "libprofiled_static_defaults",
-        "libartd-dex2oat_static_defaults",
     ],
+    static_libs: ["libdex2oatd_static"],
     target: {
         linux_glibc_x86_64: {
             use_clang_lld: true,
diff --git a/dexlayout/Android.bp b/dexlayout/Android.bp
index c21ea35..07ee8f9 100644
--- a/dexlayout/Android.bp
+++ b/dexlayout/Android.bp
@@ -58,7 +58,7 @@
 
 cc_defaults {
     name: "libart-dexlayout_static_base_defaults",
-    static_libs: [
+    whole_static_libs: [
         "libbase",
         "libz",
     ],
@@ -114,7 +114,7 @@
         "libdexfile_static_defaults",
         "libprofile_static_defaults",
     ],
-    static_libs: ["libart-dexlayout"],
+    whole_static_libs: ["libart-dexlayout"],
 }
 
 art_cc_library {
@@ -159,7 +159,7 @@
         "libdexfiled_static_defaults",
         "libprofiled_static_defaults",
     ],
-    static_libs: ["libartd-dexlayout"],
+    whole_static_libs: ["libartd-dexlayout"],
 }
 
 cc_defaults {
diff --git a/libartbase/Android.bp b/libartbase/Android.bp
index 5845266..e3ae6dd 100644
--- a/libartbase/Android.bp
+++ b/libartbase/Android.bp
@@ -122,7 +122,7 @@
 
 cc_defaults {
     name: "libartbase_static_base_defaults",
-    static_libs: [
+    whole_static_libs: [
         "libbase",
         "libartpalette",
         "liblog",
@@ -134,13 +134,13 @@
 cc_defaults {
     name: "libartbase_static_defaults",
     defaults: ["libartbase_static_base_defaults"],
-    static_libs: ["libartbase"],
+    whole_static_libs: ["libartbase"],
 }
 
 cc_defaults {
     name: "libartbased_static_defaults",
     defaults: ["libartbase_static_base_defaults"],
-    static_libs: ["libartbased"],
+    whole_static_libs: ["libartbased"],
 }
 
 gensrcs {
diff --git a/libdexfile/Android.bp b/libdexfile/Android.bp
index b24ee97..c80dc4d 100644
--- a/libdexfile/Android.bp
+++ b/libdexfile/Android.bp
@@ -102,7 +102,7 @@
 
 cc_defaults {
     name: "libdexfile_static_base_defaults",
-    static_libs: [
+    whole_static_libs: [
         "libbase",
         "liblog",
         "libz",
@@ -119,7 +119,7 @@
     defaults_visibility: [
         "//art:__subpackages__",
     ],
-    static_libs: ["libdexfile"],
+    whole_static_libs: ["libdexfile"],
 }
 
 cc_defaults {
@@ -128,7 +128,7 @@
         "libartbased_static_defaults",
         "libdexfile_static_base_defaults",
     ],
-    static_libs: ["libdexfiled"],
+    whole_static_libs: ["libdexfiled"],
 }
 
 gensrcs {
@@ -433,7 +433,7 @@
     defaults: [
         "libdexfile_static_defaults",
     ],
-    static_libs: [
+    whole_static_libs: [
         "libdexfile_external",
         "libdexfile_support_static",
     ],
@@ -444,7 +444,7 @@
     defaults: [
         "libdexfiled_static_defaults",
     ],
-    static_libs: [
+    whole_static_libs: [
         "libdexfile_support_static",
         "libdexfiled_external",
     ],
diff --git a/libprofile/Android.bp b/libprofile/Android.bp
index c30a2b2..aaf1a69 100644
--- a/libprofile/Android.bp
+++ b/libprofile/Android.bp
@@ -71,7 +71,7 @@
 
 cc_defaults {
     name: "libprofile_static_base_defaults",
-    static_libs: [
+    whole_static_libs: [
         "libbase",
         "libz",
         "libziparchive",
@@ -85,7 +85,7 @@
         "libartbase_static_defaults",
         "libdexfile_static_defaults",
     ],
-    static_libs: ["libprofile"],
+    whole_static_libs: ["libprofile"],
 }
 
 cc_defaults {
@@ -95,7 +95,7 @@
         "libartbased_static_defaults",
         "libdexfiled_static_defaults",
     ],
-    static_libs: ["libprofiled"],
+    whole_static_libs: ["libprofiled"],
 }
 
 art_cc_library {
diff --git a/runtime/Android.bp b/runtime/Android.bp
index 96172d1..f800726 100644
--- a/runtime/Android.bp
+++ b/runtime/Android.bp
@@ -465,7 +465,7 @@
 
 libart_static_cc_defaults {
     name: "libart_static_base_defaults",
-    static_libs: [
+    whole_static_libs: [
         "libartpalette",
         "libbacktrace",
         "libbase",
@@ -489,7 +489,7 @@
         "libdexfile_support_static_defaults",
         "libprofile_static_defaults",
     ],
-    static_libs: [
+    whole_static_libs: [
         "libart",
         "libelffile",
     ],
@@ -504,7 +504,7 @@
         "libdexfiled_support_static_defaults",
         "libprofiled_static_defaults",
     ],
-    static_libs: [
+    whole_static_libs: [
         "libartd",
         "libelffiled",
     ],
diff --git a/runtime/jni/jni_internal.cc b/runtime/jni/jni_internal.cc
index 9e4cc7d..a2e4f22 100644
--- a/runtime/jni/jni_internal.cc
+++ b/runtime/jni/jni_internal.cc
@@ -1915,6 +1915,17 @@
     ScopedObjectAccess soa(env);
     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
     gc::Heap* heap = Runtime::Current()->GetHeap();
+    if (heap->IsMovableObject(s)) {
+      StackHandleScope<1> hs(soa.Self());
+      HandleWrapperObjPtr<mirror::String> h(hs.NewHandleWrapper(&s));
+      if (!kUseReadBarrier) {
+        heap->IncrementDisableMovingGC(soa.Self());
+      } else {
+        // For the CC collector, we only need to wait for the thread flip rather than the whole GC
+        // to occur thanks to the to-space invariant.
+        heap->IncrementDisableThreadFlip(soa.Self());
+      }
+    }
     if (s->IsCompressed()) {
       if (is_copy != nullptr) {
         *is_copy = JNI_TRUE;
@@ -1926,17 +1937,6 @@
       }
       return chars;
     } else {
-      if (heap->IsMovableObject(s)) {
-        StackHandleScope<1> hs(soa.Self());
-        HandleWrapperObjPtr<mirror::String> h(hs.NewHandleWrapper(&s));
-        if (!kUseReadBarrier) {
-          heap->IncrementDisableMovingGC(soa.Self());
-        } else {
-          // For the CC collector, we only need to wait for the thread flip rather
-          // than the whole GC to occur thanks to the to-space invariant.
-          heap->IncrementDisableThreadFlip(soa.Self());
-        }
-      }
       if (is_copy != nullptr) {
         *is_copy = JNI_FALSE;
       }
@@ -1951,16 +1951,14 @@
     ScopedObjectAccess soa(env);
     gc::Heap* heap = Runtime::Current()->GetHeap();
     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
-    if (!s->IsCompressed() && heap->IsMovableObject(s)) {
+    if (heap->IsMovableObject(s)) {
       if (!kUseReadBarrier) {
         heap->DecrementDisableMovingGC(soa.Self());
       } else {
         heap->DecrementDisableThreadFlip(soa.Self());
       }
     }
-    // TODO: For uncompressed strings GetStringCritical() always returns `s->GetValue()`.
-    // Should we report an error if the user passes a different `chars`?
-    if (s->IsCompressed() || (!s->IsCompressed() && s->GetValue() != chars)) {
+    if (s->IsCompressed() || (s->IsCompressed() == false && s->GetValue() != chars)) {
       delete[] chars;
     }
   }