Update linkerconfig up to date

Current implementation is based on old ld.config.txt, so it should be up
to date before actually being used. This change keeps generated
ld.config.txt same with current one under /system/etc

Test: m -j && Cuttlefish, compared with ld.config.txt from latest master
Change-Id: If739c907cc21c4572d5e22edfa18f6b3d04e3aa6
diff --git a/.clang-format b/.clang-format
index 175c5dd..5023f2c 100644
--- a/.clang-format
+++ b/.clang-format
@@ -8,4 +8,7 @@
 PointerAlignment: Left
 TabWidth: 2
 UseTab: Never
-PenaltyExcessCharacter: 32
\ No newline at end of file
+PenaltyExcessCharacter: 32
+
+BinPackArguments: false
+AllowAllParametersOfDeclarationOnNextLine: false
\ No newline at end of file
diff --git a/contents/include/linkerconfig/namespacebuilder.h b/contents/include/linkerconfig/namespacebuilder.h
index d1e0ecf..6e295d9 100644
--- a/contents/include/linkerconfig/namespacebuilder.h
+++ b/contents/include/linkerconfig/namespacebuilder.h
@@ -37,6 +37,7 @@
 NamespaceBuilder BuildVndkInSystemNamespace;
 NamespaceBuilder BuildUnrestrictedDefaultNamespace;
 NamespaceBuilder BuildPostInstallNamespace;
+NamespaceBuilder BuildNeuralNetworksNamespace;
 }  // namespace contents
 }  // namespace linkerconfig
 }  // namespace android
\ No newline at end of file
diff --git a/contents/namespace/conscrypt.cc b/contents/namespace/conscrypt.cc
index 7015b37..8f258b0 100644
--- a/contents/namespace/conscrypt.cc
+++ b/contents/namespace/conscrypt.cc
@@ -22,8 +22,10 @@
 using android::linkerconfig::modules::Namespace;
 
 namespace {
-const std::vector<std::string> kLibsFromDefault = {"libc.so", "libm.so",
-                                                   "libdl.so", "liblog.so"};
+const std::vector<std::string> kLibsFromDefault = {"libc.so",
+                                                   "libm.so",
+                                                   "libdl.so",
+                                                   "liblog.so"};
 }  // namespace
 
 namespace android {
diff --git a/contents/namespace/media.cc b/contents/namespace/media.cc
index f332a41..d4d2fd3 100644
--- a/contents/namespace/media.cc
+++ b/contents/namespace/media.cc
@@ -42,8 +42,12 @@
     "libclang_rt.hwasan-aarch64-android.so"};
 
 const std::vector<std::string> kLibsFromDefault = {
-    "@{LLNDK_LIBRARIES}", "libandroid.so", "libbinder_ndk.so",
-    "libmediametrics.so", "@{SANITIZER_RUNTIME_LIBRARIES}"};
+    "@{LLNDK_LIBRARIES}",
+    "libbinder_ndk.so",
+    "libmediametrics.so",
+    "@{SANITIZER_RUNTIME_LIBRARIES}"};
+
+const std::vector<std::string> kLibsFromDefaultSystem = {"libcgrouprc.so"};
 }  // namespace
 
 namespace android {
@@ -51,13 +55,26 @@
 namespace contents {
 Namespace BuildMediaNamespace([[maybe_unused]] const Context& ctx) {
   bool is_legacy = android::linkerconfig::modules::IsLegacyDevice();
+  bool is_system_section = ctx.IsSystemSection();
+
   Namespace ns("media", /*is_isolated=*/true, /*is_visible=*/true);
   ns.AddSearchPath("/apex/com.android.media/${LIB}", /*also_in_asan=*/true,
                    /*with_data_asan=*/false);
   ns.AddPermittedPath("/apex/com.android.media/${LIB}/extractors",
-                      /*also_in_asan=*/false, /*with_data_asan=*/false);
-  ns.CreateLink("default").AddSharedLib(is_legacy ? kLibsFromDefaultLegacy
-                                                  : kLibsFromDefault);
+                      /*also_in_asan=*/true,
+                      /*with_data_asan=*/false);
+
+  auto& link_to_default = ns.CreateLink("default");
+  if (is_legacy) {
+    link_to_default.AddSharedLib(kLibsFromDefaultLegacy);
+  } else {
+    link_to_default.AddSharedLib(kLibsFromDefault);
+    if (is_system_section) {
+      link_to_default.AddSharedLib(kLibsFromDefaultSystem);
+    }
+  }
+
+  ns.CreateLink("neuralnetworks").AddSharedLib("libneuralnetworks.so");
 
   return ns;
 }
diff --git a/contents/namespace/neuralnetworks.cc b/contents/namespace/neuralnetworks.cc
new file mode 100644
index 0000000..2024842
--- /dev/null
+++ b/contents/namespace/neuralnetworks.cc
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "linkerconfig/namespacebuilder.h"
+
+#include "linkerconfig/environment.h"
+#include "linkerconfig/namespace.h"
+
+using android::linkerconfig::modules::Namespace;
+
+namespace android {
+namespace linkerconfig {
+namespace contents {
+Namespace BuildNeuralNetworksNamespace([[maybe_unused]] const Context& ctx) {
+  Namespace ns("neuralnetworks", /*is_isolated=*/true, /*is_visible=*/true);
+  ns.AddSearchPath("/apex/com.android.neuralnetworks/${LIB}",
+                   /*also_in_asan=*/true,
+                   /*with_data_asan=*/false);
+
+  ns.CreateLink("default").AddSharedLib({"libc.so",
+                                         "libcgrouprc.so",
+                                         "libdl.so",
+                                         "liblog.so",
+                                         "libm.so",
+                                         "libnativewindow.so",
+                                         "libsync.so",
+                                         "libvndksupport.so"});
+
+  return ns;
+}
+}  // namespace contents
+}  // namespace linkerconfig
+}  // namespace android
\ No newline at end of file
diff --git a/contents/namespace/postinstall.cc b/contents/namespace/postinstall.cc
index e7a1e18..86859c7 100644
--- a/contents/namespace/postinstall.cc
+++ b/contents/namespace/postinstall.cc
@@ -24,8 +24,15 @@
 Namespace BuildPostInstallNamespace([[maybe_unused]] const Context& ctx) {
   Namespace ns("default", /*is_isolated=*/false,
                /*is_visible=*/false);
-  ns.AddSearchPath("/system/${LIB}");
-  ns.AddSearchPath("/@{PRODUCT:product}/${LIB}");
+  ns.AddSearchPath("/system/${LIB}",
+                   /*also_in_asan=*/false,
+                   /*with_data_asan=*/false);
+  ns.AddSearchPath("/@{SYSTEM_EXT:system_ext}/${LIB}",
+                   /*also_in_asan=*/false,
+                   /*with_data_asan=*/false);
+  ns.AddSearchPath("/@{PRODUCT:product}/${LIB}",
+                   /*also_in_asan=*/false,
+                   /*with_data_asan=*/false);
 
   return ns;
 }
diff --git a/contents/namespace/resolv.cc b/contents/namespace/resolv.cc
index 57cae95..ba62fe9 100644
--- a/contents/namespace/resolv.cc
+++ b/contents/namespace/resolv.cc
@@ -22,12 +22,16 @@
 using android::linkerconfig::modules::Namespace;
 
 namespace {
-const std::vector<std::string> kLibsFromDefault = {
-    "libc.so",          "libcgrouprc.so", "libm.so",          "libdl.so",
-    "libbinder_ndk.so", "liblog.so",      "libvndksupport.so"};
+const std::vector<std::string> kLibsFromDefault = {"libc.so",
+                                                   "libcgrouprc.so",
+                                                   "libm.so",
+                                                   "libdl.so",
+                                                   "libbinder_ndk.so",
+                                                   "liblog.so",
+                                                   "libvndksupport.so"};
 
-const std::vector<std::string> kLibsFromUnrestrictedDefault = {
-    "libc.so", "libm.so", "libdl.so", "libbinder_ndk.so", "liblog.so"};
+const std::vector<std::string> kLibsFromUnrestrictedDefault =
+    {"libc.so", "libm.so", "libdl.so", "libbinder_ndk.so", "liblog.so"};
 }  // namespace
 
 namespace android {
diff --git a/contents/namespace/rs.cc b/contents/namespace/rs.cc
index af80515..d8630a1 100644
--- a/contents/namespace/rs.cc
+++ b/contents/namespace/rs.cc
@@ -47,6 +47,7 @@
                                          "@{SANITIZER_RUNTIME_LIBRARIES}",
                                          "@{PRIVATE_LLNDK_LIBRARIES:}"});
   ns.CreateLink("vndk").AddSharedLib("@{VNDK_SAMEPROCESS_LIBRARIES}");
+  ns.CreateLink("neuralnetworks").AddSharedLib("libneuralnetworks.so");
 
   return ns;
 }
diff --git a/contents/namespace/sphal.cc b/contents/namespace/sphal.cc
index 7e69f66..fd00a66 100644
--- a/contents/namespace/sphal.cc
+++ b/contents/namespace/sphal.cc
@@ -41,6 +41,7 @@
   ns.CreateLink("default").AddSharedLib(
       {"@{LLNDK_LIBRARIES:}", "@{SANITIZER_RUNTIME_LIBRARIES:}"});
   ns.CreateLink("vndk").AddSharedLib("@{VNDK_SAMEPROCESS_LIBRARIES:}");
+  ns.CreateLink("neuralnetworks").AddSharedLib("libneuralnetworks.so");
 
   return ns;
 }
diff --git a/contents/namespace/system.cc b/contents/namespace/system.cc
index 786bbe2..3aa4d4e 100644
--- a/contents/namespace/system.cc
+++ b/contents/namespace/system.cc
@@ -31,8 +31,17 @@
                    /*with_data_asan=*/true);
 
   ns.CreateLink("runtime").AddSharedLib(
-      {"libdexfile_external.so", "libnativebridge.so", "libnativehelper.so",
-       "libnativeloader.so", "libandroidicu.so"});
+      {"libdexfile_external.so",
+       "libdexfile_external.so",
+       "libnativebridge.so",
+       "libnativehelper.so",
+       "libnativeloader.so",
+       "libandroidicu.so",
+       // TODO(b/120786417 or b/134659294): libicuuc.so and libicui18n.so are
+       // kept for app compat.
+       "libicui18n.so",
+       "libicuuc.so",
+       "@{SANITIZER_RUNTIME_LIBRARIES}"});
 
   return ns;
 }
diff --git a/contents/namespace/systemdefault.cc b/contents/namespace/systemdefault.cc
index 8dfc18c..4fa1011 100644
--- a/contents/namespace/systemdefault.cc
+++ b/contents/namespace/systemdefault.cc
@@ -23,28 +23,60 @@
 
 namespace {
 const std::vector<std::string> kLibsFromRuntimeLegacy = {
-    "libart.so:libartd.so", "libdexfile_external.so", "libnativebridge.so",
-    "libnativehelper.so", "libnativeloader.so", "libandroidicu.so",
+    "libart.so:libartd.so",
+    "libdexfile_external.so",
+    "libnativebridge.so",
+    "libnativehelper.so",
+    "libnativeloader.so",
+    "libandroidicu.so",
     // TODO(b/122876336): Remove libpac.so once it's migrated to Webview
     "libpac.so"};
 
 const std::vector<std::string> kLibsFromRuntime = {
-    "libdexfile_external.so", "libnativebridge.so", "libnativehelper.so",
-    "libnativeloader.so", "libandroidicu.so"};
+    "libdexfile_external.so",
+    "libdexfiled_external.so",
+    "libnativebridge.so",
+    "libnativehelper.so",
+    "libnativeloader.so",
+    "libandroidicu.so",
+    "libpac.so",
+    "@{SANITIZER_RUNTIME_LIBRARIES}",
+    // TODO(b/120786417 or b/134659294): libicuuc.so and libicui18n.so are kept
+    // for app compat.
+    "libicui18n.so",
+    "libicuuc.so"};
 
 const std::vector<std::string> kPermittedPaths = {
-    "/system/${LIB}/drm", "/system/${LIB}/extractors", "/system/${LIB}/hw",
-    "/@{SYSTEM_EXT:system_ext}/${LIB}", "/@{PRODUCT:product}/${LIB}",
+    "/system/${LIB}/drm",
+    "/system/${LIB}/extractors",
+    "/system/${LIB}/hw",
+    "/@{SYSTEM_EXT:system_ext}/${LIB}",
+    "/@{PRODUCT:product}/${LIB}",
     // These are where odex files are located. libart has to be able to
     // dlopen the files
-    "/system/framework", "/system/app", "/system/priv-app",
-    "/@{SYSTEM_EXT:system_ext}/framework", "/@{SYSTEM_EXT:system_ext}/app",
-    "/@{SYSTEM_EXT:system_ext}/priv-app", "/vendor/framework", "/vendor/app",
-    "/vendor/priv-app", "/system/vendor/framework", "/system/vendor/app",
-    "/system/vendor/priv-app", "/odm/framework", "/odm/app", "/odm/priv-app",
-    "/oem/app", "/@{PRODUCT:product}/framework", "/@{PRODUCT:product}/app",
-    "/@{PRODUCT:product}/priv-app", "/data", "/mnt/expand",
-    "/apex/com.android.runtime/${LIB}/bionic", "/system/${LIB}/bootstrap"};
+    "/system/framework",
+    "/system/app",
+    "/system/priv-app",
+    "/@{SYSTEM_EXT:system_ext}/framework",
+    "/@{SYSTEM_EXT:system_ext}/app",
+    "/@{SYSTEM_EXT:system_ext}/priv-app",
+    "/vendor/framework",
+    "/vendor/app",
+    "/vendor/priv-app",
+    "/system/vendor/framework",
+    "/system/vendor/app",
+    "/system/vendor/priv-app",
+    "/odm/framework",
+    "/odm/app",
+    "/odm/priv-app",
+    "/oem/app",
+    "/@{PRODUCT:product}/framework",
+    "/@{PRODUCT:product}/app",
+    "/@{PRODUCT:product}/priv-app",
+    "/data",
+    "/mnt/expand",
+    "/apex/com.android.runtime/${LIB}/bionic",
+    "/system/${LIB}/bootstrap"};
 
 void BuildPermittedPath(Namespace& ns) {
   for (const auto& path : kPermittedPaths) {
@@ -81,6 +113,7 @@
   ns.CreateLink("runtime").AddSharedLib(is_legacy ? kLibsFromRuntimeLegacy
                                                   : kLibsFromRuntime);
   ns.CreateLink("resolv").AddSharedLib("libnetd_resolv.so");
+  ns.CreateLink("neuralnetworks").AddSharedLib("libneuralnetworks.so");
 
   return ns;
 }
diff --git a/contents/namespace/unrestricteddefault.cc b/contents/namespace/unrestricteddefault.cc
index a329f14..9b11626 100644
--- a/contents/namespace/unrestricteddefault.cc
+++ b/contents/namespace/unrestricteddefault.cc
@@ -23,8 +23,18 @@
 
 namespace {
 const std::vector<std::string> kLibsFromRuntime = {
-    "libdexfile_external.so", "libnativebridge.so", "libnativehelper.so",
-    "libnativeloader.so",     "libandroidicu.so",   "libpac.so"};
+    "libdexfile_external.so",
+    "libdexfiled_external.so",
+    "libnativebridge.so",
+    "libnativehelper.so",
+    "libnativeloader.so",
+    "libandroidicu.so",
+    "libpac.so",
+    // TODO(b/120786417 or b/134659294): libicuuc.so and libicui18n.so are kept
+    // for app compat.
+    "libicui18n.so",
+    "libicuuc.so",
+    "@{SANITIZER_RUNTIME_LIBRARIES}"};
 }  // namespace
 
 namespace android {
@@ -42,6 +52,7 @@
 
   ns.CreateLink("runtime").AddSharedLib(kLibsFromRuntime);
   ns.CreateLink("resolv").AddSharedLib("libnetd_resolv.so");
+  ns.CreateLink("neuralnetworks").AddSharedLib("libneuralnetworks.so");
 
   return ns;
 }
diff --git a/contents/namespace/vendordefault.cc b/contents/namespace/vendordefault.cc
index 53b2353..ebf329f 100644
--- a/contents/namespace/vendordefault.cc
+++ b/contents/namespace/vendordefault.cc
@@ -44,13 +44,16 @@
   ns.AddPermittedPath("/system/vendor", /*also_in_asan=*/false,
                       /*with_data_asan=*/false);
 
-  ns.CreateLink("system").AddSharedLib("@{LLNDK_LIBRARIES}");
+  ns.CreateLink("runtime").AddSharedLib("@{SANITIZER_RUNTIME_LIBRARIES}");
+  ns.CreateLink("system").AddSharedLib(
+      {"@{LLNDK_LIBRARIES}", "@{SANITIZER_RUNTIME_LIBRARIES}"});
   ns.CreateLink("vndk").AddSharedLib(
       {"@{VNDK_SAMEPROCESS_LIBRARIES}", "@{VNDK_CORE_LIBRARIES}"});
   if (android::linkerconfig::modules::IsVndkInSystemNamespace()) {
     ns.CreateLink("vndk_in_system")
         .AddSharedLib("@{VNDK_USING_CORE_VARIANT_LIBRARIES}");
   }
+  ns.CreateLink("neuralnetworks").AddSharedLib("libneuralnetworks.so");
 
   return ns;
 }
diff --git a/contents/namespace/vndk.cc b/contents/namespace/vndk.cc
index 3de3175..7241861 100644
--- a/contents/namespace/vndk.cc
+++ b/contents/namespace/vndk.cc
@@ -25,7 +25,9 @@
 namespace contents {
 Namespace BuildVndkNamespace([[maybe_unused]] const Context& ctx) {
   bool is_system_section = ctx.IsSystemSection();
-  Namespace ns("vndk", /*is_isolated=*/true, /*is_visible=*/true);
+  Namespace ns("vndk",
+               /*is_isolated=*/is_system_section,
+               /*is_visible=*/is_system_section);
 
   ns.AddSearchPath("/odm/${LIB}/vndk-sp", /*also_in_asan=*/true,
                    /*with_data_asan=*/true);
@@ -62,6 +64,8 @@
 
   ns.CreateLink(is_system_section ? "default" : "system")
       .AddSharedLib({"@{LLNDK_LIBRARIES}", "@{SANITIZER_RUNTIME_LIBRARIES}"});
+  ns.CreateLink("runtime").AddSharedLib("@{SANITIZER_RUNTIME_LIBRARIES}");
+
   if (is_system_section) {
     ns.CreateLink("sphal", true);
   } else {
@@ -73,6 +77,8 @@
     }
   }
 
+  ns.CreateLink("neuralnetworks").AddSharedLib("libneuralnetworks.so");
+
   return ns;
 }
 }  // namespace contents
diff --git a/contents/namespace/vndkinsystem.cc b/contents/namespace/vndkinsystem.cc
index bddf76b..b6b10e7 100644
--- a/contents/namespace/vndkinsystem.cc
+++ b/contents/namespace/vndkinsystem.cc
@@ -41,6 +41,8 @@
   ns.CreateLink("system").AddSharedLib(
       {"@{LLNDK_LIBRARIES}", "@{SANITIZER_RUNTIME_LIBRARIES}"});
   ns.CreateLink("vndk", true);
+  ns.CreateLink("runtime").AddSharedLib("@{SANITIZER_RUNTIME_LIBRARIES}");
+  ns.CreateLink("neuralnetworks").AddSharedLib("libneuralnetworks.so");
 
   return ns;
 }
diff --git a/contents/section/legacy.cc b/contents/section/legacy.cc
index 20281f0..a8d74fd 100644
--- a/contents/section/legacy.cc
+++ b/contents/section/legacy.cc
@@ -25,7 +25,11 @@
 
 namespace {
 const std::vector<std::string> kLegacyBinaryPath = {
-    "/system", "/product", "/vendor", "/odm", "/sbin",
+    "/system",
+    "/product",
+    "/vendor",
+    "/odm",
+    "/sbin",
 };
 }  // namespace
 
@@ -41,6 +45,7 @@
   namespaces.emplace_back(BuildMediaNamespace(ctx));
   namespaces.emplace_back(BuildConscryptNamespace(ctx));
   namespaces.emplace_back(BuildResolvNamespace(ctx));
+  namespaces.emplace_back(BuildNeuralNetworksNamespace(ctx));
 
   return Section("legacy", kLegacyBinaryPath, std::move(namespaces));
 }
diff --git a/contents/section/system.cc b/contents/section/system.cc
index e798e8f..db1ed7c 100644
--- a/contents/section/system.cc
+++ b/contents/section/system.cc
@@ -28,6 +28,7 @@
 const std::vector<std::string> kBinaryPath = {
     "/system/bin/",
     "/system/xbin/",
+    "/@{SYSTEM_EXT:system_ext}/bin/",
     "/@{PRODUCT:product}/bin/",
     "/data",
 };
@@ -48,6 +49,7 @@
   namespaces.emplace_back(BuildSphalNamespace(ctx));
   namespaces.emplace_back(BuildRsNamespace(ctx));
   namespaces.emplace_back(BuildVndkNamespace(ctx));
+  namespaces.emplace_back(BuildNeuralNetworksNamespace(ctx));
 
   return Section("system", kBinaryPath, std::move(namespaces));
 }
diff --git a/contents/section/unrestricted.cc b/contents/section/unrestricted.cc
index 2cc8b20..e79c659 100644
--- a/contents/section/unrestricted.cc
+++ b/contents/section/unrestricted.cc
@@ -44,6 +44,7 @@
   namespaces.emplace_back(BuildMediaNamespace(ctx));
   namespaces.emplace_back(BuildConscryptNamespace(ctx));
   namespaces.emplace_back(BuildResolvNamespace(ctx));
+  namespaces.emplace_back(BuildNeuralNetworksNamespace(ctx));
 
   return Section("unrestricted", kBinaryPath, std::move(namespaces));
 }
diff --git a/contents/section/vendor.cc b/contents/section/vendor.cc
index 07ebf5b..54517ad 100644
--- a/contents/section/vendor.cc
+++ b/contents/section/vendor.cc
@@ -50,7 +50,11 @@
   namespaces.emplace_back(BuildRuntimeNamespace(ctx));
   namespaces.emplace_back(BuildVndkNamespace(ctx));
   namespaces.emplace_back(BuildSystemNamespace(ctx));
-  namespaces.emplace_back(BuildVndkInSystemNamespace(ctx));
+  namespaces.emplace_back(BuildNeuralNetworksNamespace(ctx));
+
+  if (android::linkerconfig::modules::IsVndkInSystemNamespace()) {
+    namespaces.emplace_back(BuildVndkInSystemNamespace(ctx));
+  }
 
   return Section("vendor", kBinaryPath, std::move(namespaces));
 }