Fix bootstrap linker library searching

For the bootstrap linker, insert /system/${LIB}/bootstrap in front of
/system/${LIB} in any namespace search path.

Bug: http://b/152572170
Test: bionic unit tests
Change-Id: Ia359d9f2063f4b6fff3f79b51b500ba968a18247
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 090e7f0..e34c796 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -1062,14 +1062,6 @@
     }
   }
 
-  // For the bootstrap linker, search for the bootstrap bionic libraries (e.g. libc.so).
-#if !defined(__ANDROID_APEX__)
-  if (fd == -1) {
-    std::vector<std::string> bootstrap_paths = { std::string(kSystemLibDir) + "/bootstrap" };
-    fd = open_library_on_paths(zip_archive_cache, name, file_offset, bootstrap_paths, realpath);
-  }
-#endif
-
   // Finally search the namespace's main search path list.
   if (fd == -1) {
     fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
@@ -2424,6 +2416,21 @@
   }
 }
 
+std::vector<std::string> fix_lib_paths(std::vector<std::string> paths) {
+  // For the bootstrap linker, insert /system/${LIB}/bootstrap in front of /system/${LIB} in any
+  // namespace search path. The bootstrap linker should prefer to use the bootstrap bionic libraries
+  // (e.g. libc.so).
+#if !defined(__ANDROID_APEX__)
+  for (size_t i = 0; i < paths.size(); ++i) {
+    if (paths[i] == kSystemLibDir) {
+      paths.insert(paths.begin() + i, std::string(kSystemLibDir) + "/bootstrap");
+      ++i;
+    }
+  }
+#endif
+  return paths;
+}
+
 android_namespace_t* create_namespace(const void* caller_addr,
                                       const char* name,
                                       const char* ld_library_path,
diff --git a/linker/linker_namespaces.h b/linker/linker_namespaces.h
index 9561bb4..6843ebc 100644
--- a/linker/linker_namespaces.h
+++ b/linker/linker_namespaces.h
@@ -34,6 +34,8 @@
 #include <vector>
 #include <unordered_set>
 
+std::vector<std::string> fix_lib_paths(std::vector<std::string> paths);
+
 struct android_namespace_t;
 
 struct android_namespace_link_t {
@@ -100,10 +102,10 @@
     return default_library_paths_;
   }
   void set_default_library_paths(std::vector<std::string>&& library_paths) {
-    default_library_paths_ = std::move(library_paths);
+    default_library_paths_ = fix_lib_paths(std::move(library_paths));
   }
   void set_default_library_paths(const std::vector<std::string>& library_paths) {
-    default_library_paths_ = library_paths;
+    default_library_paths_ = fix_lib_paths(library_paths);
   }
 
   const std::vector<std::string>& get_permitted_paths() const {