Extend Link() to allow linking to the default namespace.

The code path through native bridge and NDK translation should
propagate nullptr as well.

This to support a later change that changes the way native test
libraries are loaded in ART run tests.

Test: art/test/testrunner/testrunner.py --target --64 --optimizing
Bug: 130340935
Change-Id: I934d11942e41ccc6d140a7044faa160b006166f1
diff --git a/libnativeloader/library_namespaces.cpp b/libnativeloader/library_namespaces.cpp
index dcd2f65..79fee06 100644
--- a/libnativeloader/library_namespaces.cpp
+++ b/libnativeloader/library_namespaces.cpp
@@ -305,7 +305,7 @@
     return system_ns.error();
   }
 
-  auto linked = app_ns->Link(*system_ns, system_exposed_libraries);
+  auto linked = app_ns->Link(&system_ns.value(), system_exposed_libraries);
   if (!linked.ok()) {
     return linked.error();
   }
@@ -314,7 +314,7 @@
     auto ns = NativeLoaderNamespace::GetExportedNamespace(apex_ns_name, is_bridged);
     // Even if APEX namespace is visible, it may not be available to bridged.
     if (ns.ok()) {
-      linked = app_ns->Link(*ns, public_libs);
+      linked = app_ns->Link(&ns.value(), public_libs);
       if (!linked.ok()) {
         return linked.error();
       }
@@ -325,7 +325,7 @@
   if (unbundled_app_origin == APK_ORIGIN_VENDOR && !vndksp_libraries_vendor().empty()) {
     auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkNamespaceName, is_bridged);
     if (vndk_ns.ok()) {
-      linked = app_ns->Link(*vndk_ns, vndksp_libraries_vendor());
+      linked = app_ns->Link(&vndk_ns.value(), vndksp_libraries_vendor());
       if (!linked.ok()) {
         return linked.error();
       }
@@ -336,7 +336,7 @@
   if (unbundled_app_origin == APK_ORIGIN_PRODUCT && !vndksp_libraries_product().empty()) {
     auto vndk_ns = NativeLoaderNamespace::GetExportedNamespace(kVndkProductNamespaceName, is_bridged);
     if (vndk_ns.ok()) {
-      linked = app_ns->Link(*vndk_ns, vndksp_libraries_product());
+      linked = app_ns->Link(&vndk_ns.value(), vndksp_libraries_product());
       if (!linked.ok()) {
         return linked.error();
       }
@@ -349,7 +349,7 @@
     if (jni_libs != "") {
       auto apex_ns = NativeLoaderNamespace::GetExportedNamespace(*apex_ns_name, is_bridged);
       if (apex_ns.ok()) {
-        linked = app_ns->Link(*apex_ns, jni_libs);
+        linked = app_ns->Link(&apex_ns.value(), jni_libs);
         if (!linked.ok()) {
           return linked.error();
         }
@@ -364,7 +364,7 @@
     // when vendor_ns is not configured, link to the system namespace
     auto target_ns = vendor_ns.ok() ? vendor_ns : system_ns;
     if (target_ns.ok()) {
-      linked = app_ns->Link(*target_ns, vendor_libs);
+      linked = app_ns->Link(&target_ns.value(), vendor_libs);
       if (!linked.ok()) {
         return linked.error();
       }
diff --git a/libnativeloader/native_loader_namespace.cpp b/libnativeloader/native_loader_namespace.cpp
index fe15c70..669fa74 100644
--- a/libnativeloader/native_loader_namespace.cpp
+++ b/libnativeloader/native_loader_namespace.cpp
@@ -139,18 +139,20 @@
                 is_bridged ? "bridged" : "native", name, search_paths, permitted_paths);
 }
 
-Result<void> NativeLoaderNamespace::Link(const NativeLoaderNamespace& target,
+Result<void> NativeLoaderNamespace::Link(const NativeLoaderNamespace* target,
                                          const std::string& shared_libs) const {
   LOG_ALWAYS_FATAL_IF(shared_libs.empty(), "empty share lib when linking %s to %s",
-                      this->name().c_str(), target.name().c_str());
+                      this->name().c_str(), target == nullptr ? "default" : target->name().c_str());
   if (!IsBridged()) {
-    if (android_link_namespaces(this->ToRawAndroidNamespace(), target.ToRawAndroidNamespace(),
+    if (android_link_namespaces(this->ToRawAndroidNamespace(),
+                                target == nullptr ? nullptr : target->ToRawAndroidNamespace(),
                                 shared_libs.c_str())) {
       return {};
     }
   } else {
     if (NativeBridgeLinkNamespaces(this->ToRawNativeBridgeNamespace(),
-                                   target.ToRawNativeBridgeNamespace(), shared_libs.c_str())) {
+                                   target == nullptr ? nullptr : target->ToRawNativeBridgeNamespace(),
+                                   shared_libs.c_str())) {
       return {};
     }
   }
diff --git a/libnativeloader/native_loader_namespace.h b/libnativeloader/native_loader_namespace.h
index f6a324b..9a5aeac 100644
--- a/libnativeloader/native_loader_namespace.h
+++ b/libnativeloader/native_loader_namespace.h
@@ -55,7 +55,11 @@
   std::string name() const { return name_; }
   bool IsBridged() const { return raw_.index() == 1; }
 
-  Result<void> Link(const NativeLoaderNamespace& target, const std::string& shared_libs) const;
+  // Creates a link from this namespace to target for the ":"-separated list of
+  // libraries in shared_libs. If target is nullptr it creates a link to the
+  // default namespace.
+  Result<void> Link(const NativeLoaderNamespace* target, const std::string& shared_libs) const;
+
   Result<void*> Load(const char* lib_name) const;
 
   static Result<NativeLoaderNamespace> GetExportedNamespace(const std::string& name,