Define product public libraries

When /product is unbundled from the /system, /product/etc/ may not
have 'public.libraries-<companyname>.txt' to extend public libraries.
Instead, /product/etc/public.library.txt can provide public libraries
from /product.

Bug: 186055799
Test: atest libnativeloader_test
Change-Id: I8994649826657f59ac1dac655205b9704a2c67c9
diff --git a/libnativeloader/library_namespaces.cpp b/libnativeloader/library_namespaces.cpp
index 8b87338..585e98a 100644
--- a/libnativeloader/library_namespaces.cpp
+++ b/libnativeloader/library_namespaces.cpp
@@ -47,7 +47,12 @@
 // to use to load vendor libraries to separate namespace with controlled interface between
 // vendor and system namespaces.
 constexpr const char* kVendorNamespaceName = "sphal";
+// Similar to sphal namespace, product namespace provides some product libraries.
+constexpr const char* kProductNamespaceName = "product";
+
+// vndk namespace for unbundled vendor apps
 constexpr const char* kVndkNamespaceName = "vndk";
+// vndk_product namespace for unbundled product apps
 constexpr const char* kVndkProductNamespaceName = "vndk_product";
 
 // classloader-namespace is a linker namespace that is created for the loaded
@@ -374,6 +379,27 @@
     }
   }
 
+  auto product_libs = filter_public_libraries(target_sdk_version, uses_libraries,
+                                              product_public_libraries());
+  if (!product_libs.empty()) {
+    auto target_ns = system_ns;
+    if (is_product_vndk_version_defined()) {
+      // If ro.product.vndk.version is defined, product namespace provides the product libraries.
+      target_ns = NativeLoaderNamespace::GetExportedNamespace(kProductNamespaceName, is_bridged);
+    }
+    if (target_ns.ok()) {
+      linked = app_ns->Link(&target_ns.value(), product_libs);
+      if (!linked.ok()) {
+        return linked.error();
+      }
+    } else {
+      // The linkerconfig must have a problem on defining the product namespace in the system
+      // section. Skip linking product namespace. This will not affect most of the apps. Only the
+      // apps that requires the product public libraries will fail.
+      ALOGW("Namespace for product libs not found: %s", target_ns.error().message().c_str());
+    }
+  }
+
   auto& emplaced = namespaces_.emplace_back(
       std::make_pair(env->NewWeakGlobalRef(class_loader), *app_ns));
   if (is_main_classloader) {