Move test libraries under /data/nativetests

Move libraries used in bionic-unit-tests out of system partition to
/data/nativetests../bionic-loader-test-libs

Bug: http://b/22182538
Test: build bionic and run bionic-unit-tests
Change-Id: I170177bef782839d0b4970ae4418bf54d0a77836
diff --git a/tests/Android.bp b/tests/Android.bp
index 0bbe4e3..ed42bde 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -151,6 +151,9 @@
         "libbase",
     ],
     host_ldlibs: ["-lrt"],
+    shared: {
+        enabled: false,
+    },
 }
 
 // -----------------------------------------------------------------------------
@@ -179,6 +182,9 @@
         "-D_FORTIFY_SOURCE=1",
         "-DTEST_NAME=Fortify1_gcc"
     ],
+    shared: {
+        enabled: false,
+    },
 }
 
 cc_test_library {
@@ -189,6 +195,9 @@
         "-D_FORTIFY_SOURCE=2",
         "-DTEST_NAME=Fortify2_gcc"
     ],
+    shared: {
+        enabled: false,
+    },
 }
 
 cc_test_library {
@@ -199,6 +208,9 @@
         "-D_FORTIFY_SOURCE=1",
         "-DTEST_NAME=Fortify1_clang"
     ],
+    shared: {
+        enabled: false,
+    },
 }
 
 cc_test_library {
@@ -209,6 +221,9 @@
         "-D_FORTIFY_SOURCE=2",
         "-DTEST_NAME=Fortify2_clang"
     ],
+    shared: {
+        enabled: false,
+    },
 }
 
 // -----------------------------------------------------------------------------
@@ -226,7 +241,7 @@
     ],
     shared: {
         enabled: false,
-    }
+    },
 }
 
 // -----------------------------------------------------------------------------
@@ -241,6 +256,9 @@
             enabled: true,
         },
     },
+    shared: {
+        enabled: false,
+    },
 }
 
 // -----------------------------------------------------------------------------
@@ -252,6 +270,9 @@
     defaults: ["bionic_tests_defaults"],
     srcs: ["gtest_main.cpp"],
     cppflags: ["-DUSING_GTEST_OUTPUT_FORMAT"],
+    shared: {
+        enabled: false,
+    },
 }
 
 // -----------------------------------------------------------------------------
@@ -313,6 +334,10 @@
                 "libz",
                 "libutils",
             ],
+            ldflags: [
+                "-Wl,--rpath,\\$${ORIGIN}/../bionic-loader-test-libs",
+                "-Wl,--enable-new-dtags",
+            ],
         },
     }
 }
diff --git a/tests/Android.build.prebuilt.mk b/tests/Android.build.prebuilt.mk
index db2dfe4..c98aee2 100644
--- a/tests/Android.build.prebuilt.mk
+++ b/tests/Android.build.prebuilt.mk
@@ -17,8 +17,8 @@
 include $(CLEAR_VARS)
 LOCAL_MULTILIB := both
 LOCAL_MODULE := $(bionic_tests_module)
-LOCAL_MODULE_PATH_32 := $($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_DATA_NATIVE_TESTS)/prebuilt-elf-files
-LOCAL_MODULE_PATH_64 := $(TARGET_OUT_DATA_NATIVE_TESTS)/prebuilt-elf-files
+LOCAL_MODULE_PATH_32 := $($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_DATA_NATIVE_TESTS)/bionic-loader-test-libs/prebuilt-elf-files
+LOCAL_MODULE_PATH_64 := $(TARGET_OUT_DATA_NATIVE_TESTS)/bionic-loader-test-libs/prebuilt-elf-files
 LOCAL_MODULE_CLASS := EXECUTABLES
 
 LOCAL_SRC_FILES_arm := prebuilt-elf-files/arm/$(bionic_tests_module)
@@ -28,3 +28,4 @@
 LOCAL_SRC_FILES_mips := prebuilt-elf-files/mips/$(bionic_tests_module)
 LOCAL_SRC_FILES_mips64 := prebuilt-elf-files/mips64/$(bionic_tests_module)
 include $(BUILD_PREBUILT)
+bionic-loader-test-libs-target: $(LOCAL_MODULE)
diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp
index c3230e7..f597e61 100644
--- a/tests/dlext_test.cpp
+++ b/tests/dlext_test.cpp
@@ -55,9 +55,9 @@
 constexpr auto LIBSIZE = 1024 * 1024; // how much address space to reserve for it
 
 #if defined(__LP64__)
-#define NATIVE_TESTS_PATH "/nativetest64"
+#define NATIVE_TESTS_PATH "/nativetest64/bionic-loader-test-libs"
 #else
-#define NATIVE_TESTS_PATH "/nativetest"
+#define NATIVE_TESTS_PATH "/nativetest/bionic-loader-test-libs"
 #endif
 
 #define LIBPATH NATIVE_TESTS_PATH "/libdlext_test_fd/libdlext_test_fd.so"
@@ -150,11 +150,9 @@
 }
 
 TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
-  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
   // lib_path is relative when $ANDROID_DATA is relative
-  char lib_realpath_buf[PATH_MAX];
-  ASSERT_TRUE(realpath(lib_path.c_str(), lib_realpath_buf) == lib_realpath_buf);
-  const std::string lib_realpath = std::string(lib_realpath_buf);
+  std::string lib_path;
+  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")) + LIBZIPPATH, &lib_path)) << strerror(errno);
 
   android_dlextinfo extinfo;
   extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
@@ -179,7 +177,7 @@
   extinfo.library_fd_offset = 0;
   handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo);
   ASSERT_TRUE(handle_ == nullptr);
-  ASSERT_EQ("dlopen failed: \"" + lib_realpath + "\" has bad ELF magic", dlerror());
+  ASSERT_EQ("dlopen failed: \"" + lib_path + "\" has bad ELF magic", dlerror());
 
   // Check if dlsym works after unsuccessful dlopen().
   // Supply non-exiting one to make linker visit every soinfo.
@@ -235,7 +233,8 @@
 }
 
 TEST(dlfcn, dlopen_from_zip_absolute_path) {
-  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
+  std::string lib_path;
+  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")) + LIBZIPPATH, &lib_path)) << strerror(errno);
 
   void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
   ASSERT_TRUE(handle != nullptr) << dlerror();
@@ -248,7 +247,9 @@
 }
 
 TEST(dlfcn, dlopen_from_zip_with_dt_runpath) {
-  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH_WITH_RUNPATH;
+  std::string data_path;
+  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
+  const std::string lib_path = data_path + LIBZIPPATH_WITH_RUNPATH;
 
   void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
 
@@ -266,7 +267,9 @@
 }
 
 TEST(dlfcn, dlopen_from_zip_ld_library_path) {
-  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!/libdir";
+  std::string data_path;
+  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
+  const std::string lib_path = data_path + LIBZIPPATH + "!/libdir";
 
   typedef void (*fn_t)(const char*);
   fn_t android_update_LD_LIBRARY_PATH =
@@ -1110,7 +1113,9 @@
   static const char* root_lib = "libnstest_root.so";
   std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
 
-  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
+  std::string data_path;
+  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
+  const std::string lib_path = data_path + NATIVE_TESTS_PATH;
 
   const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
   void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index f474ed9..34c4108 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -1129,8 +1129,20 @@
   dlclose(handle);
 }
 
+// Bionic specific tests
+#if defined(__BIONIC__)
+
+#if defined(__LP64__)
+#define NATIVE_TESTS_PATH "/nativetest64/bionic-loader-test-libs"
+#else
+#define NATIVE_TESTS_PATH "/nativetest/bionic-loader-test-libs"
+#endif
+
+#define PREBUILT_ELF_PATH NATIVE_TESTS_PATH "/prebuilt-elf-files"
+
 TEST(dlfcn, dt_runpath_absolute_path) {
-  void* handle = dlopen(PATH_TO_SYSTEM_LIB "libtest_dt_runpath_d.so", RTLD_NOW);
+  std::string libpath = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH + "/libtest_dt_runpath_d.so";
+  void* handle = dlopen(libpath.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle != nullptr) << dlerror();
 
   typedef void *(* dlopen_b_fn)();
@@ -1143,19 +1155,10 @@
   dlclose(handle);
 }
 
-// Bionic specific tests
-#if defined(__BIONIC__)
-
-#if defined(__LP64__)
-#define NATIVE_TESTS_PATH "/nativetest64"
-#else
-#define NATIVE_TESTS_PATH "/nativetest"
-#endif
-
-#define PREBUILT_ELF_PATH NATIVE_TESTS_PATH "/prebuilt-elf-files"
-
 TEST(dlfcn, dlopen_invalid_rw_load_segment) {
-  std::string libpath = std::string(getenv("ANDROID_DATA")) + PREBUILT_ELF_PATH + "/libtest_invalid-rw_load_segment.so";
+  std::string data_path;
+  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
+  const std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-rw_load_segment.so";
   void* handle = dlopen(libpath.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle == nullptr);
   std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W + E load segments are not allowed";
@@ -1163,7 +1166,9 @@
 }
 
 TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
-  std::string libpath = std::string(getenv("ANDROID_DATA")) + PREBUILT_ELF_PATH + "/libtest_invalid-unaligned_shdr_offset.so";
+  std::string data_path;
+  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
+  const std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-unaligned_shdr_offset.so";
   void* handle = dlopen(libpath.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle == nullptr);
   std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: ";
@@ -1171,7 +1176,9 @@
 }
 
 TEST(dlfcn, dlopen_invalid_zero_shentsize) {
-  std::string libpath = std::string(getenv("ANDROID_DATA")) + PREBUILT_ELF_PATH + "/libtest_invalid-zero_shentsize.so";
+  std::string data_path;
+  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
+  const std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-zero_shentsize.so";
   void* handle = dlopen(libpath.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle == nullptr);
   std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has unsupported e_shentsize: 0x0 (expected 0x";
@@ -1179,7 +1186,9 @@
 }
 
 TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
-  std::string libpath = std::string(getenv("ANDROID_DATA")) + PREBUILT_ELF_PATH + "/libtest_invalid-zero_shstrndx.so";
+  std::string data_path;
+  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
+  const std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-zero_shstrndx.so";
   void* handle = dlopen(libpath.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle == nullptr);
   std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid e_shstrndx";
@@ -1187,7 +1196,9 @@
 }
 
 TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
-  std::string libpath = std::string(getenv("ANDROID_DATA")) + PREBUILT_ELF_PATH + "/libtest_invalid-empty_shdr_table.so";
+  std::string data_path;
+  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
+  std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-empty_shdr_table.so";
   void* handle = dlopen(libpath.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle == nullptr);
   std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has no section headers";
@@ -1195,7 +1206,9 @@
 }
 
 TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
-  std::string libpath = std::string(getenv("ANDROID_DATA")) + PREBUILT_ELF_PATH + "/libtest_invalid-zero_shdr_table_offset.so";
+  std::string data_path;
+  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
+  const std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-zero_shdr_table_offset.so";
   void* handle = dlopen(libpath.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle == nullptr);
   std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" has invalid shdr offset/size: 0/";
@@ -1203,7 +1216,9 @@
 }
 
 TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
-  std::string libpath = std::string(getenv("ANDROID_DATA")) + PREBUILT_ELF_PATH + "/libtest_invalid-zero_shdr_table_content.so";
+  std::string data_path;
+  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno);
+  const std::string libpath = data_path + PREBUILT_ELF_PATH + "/libtest_invalid-zero_shdr_table_content.so";
   void* handle = dlopen(libpath.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle == nullptr);
   std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\" .dynamic section header was not found";
diff --git a/tests/gtest_main.cpp b/tests/gtest_main.cpp
index 4abd37e..f2b7edd 100644
--- a/tests/gtest_main.cpp
+++ b/tests/gtest_main.cpp
@@ -55,6 +55,16 @@
   return g_executable_path;
 }
 
+bool get_realpath(const std::string& path, std::string* real_path) {
+  char realpath_buf[PATH_MAX];
+  if (realpath(path.c_str(), realpath_buf) != realpath_buf) {
+    return false;
+  }
+
+  *real_path = realpath_buf;
+  return true;
+}
+
 int get_argc() {
   return g_argc;
 }
diff --git a/tests/libs/Android.bp b/tests/libs/Android.bp
index 0303138..9ca1476 100644
--- a/tests/libs/Android.bp
+++ b/tests/libs/Android.bp
@@ -17,6 +17,12 @@
 cc_defaults {
     name: "bionic_testlib_defaults",
     host_supported: true,
+    ldflags: [
+        "-Wl,--rpath,\\$${ORIGIN}",
+        "-Wl,--enable-new-dtags",
+    ],
+    relative_install_path: "bionic-loader-test-libs",
+    gtest: false,
     sanitize: {
         never: true,
     },
@@ -30,7 +36,7 @@
 // -----------------------------------------------------------------------------
 // Library to test gnu-styled hash
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libgnu-hash-table-library",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlext_test_library.cpp"],
@@ -48,7 +54,7 @@
 // -----------------------------------------------------------------------------
 // Library to test sysv-styled hash
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libsysv-hash-table-library",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlext_test_library.cpp"],
@@ -63,8 +69,9 @@
 // -----------------------------------------------------------------------------
 // Library used by dlext tests - without GNU RELRO program header
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libdlext_test_norelro",
+    defaults: ["bionic_testlib_defaults"],
     srcs: ["dlext_test_library.cpp"],
     ldflags: ["-Wl,-z,norelro"],
     shared_libs = ["libtest_simple"],
@@ -93,7 +100,7 @@
 // -----------------------------------------------------------------------------
 // Library used by dlfcn tests
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_simple",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_testlib_simple.cpp"],
@@ -102,7 +109,7 @@
 // -----------------------------------------------------------------------------
 // Library used by dlfcn nodelete tests
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_nodelete_1",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_nodelete_1.cpp"],
@@ -111,7 +118,7 @@
 // -----------------------------------------------------------------------------
 // Library used by dlfcn nodelete tests
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_nodelete_2",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_nodelete_2.cpp"],
@@ -120,7 +127,7 @@
 // -----------------------------------------------------------------------------
 // Library used by dlfcn nodelete tests
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_nodelete_dt_flags_1",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_nodelete_dt_flags_1.cpp"],
@@ -172,7 +179,7 @@
 //
 // libtest_with_dependency_loop -> a -> b -> c -> a
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_with_dependency_loop",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_testlib_loopy_root.cpp"],
@@ -182,7 +189,7 @@
 // -----------------------------------------------------------------------------
 // libtest_with_dependency_loop_a.so
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_with_dependency_loop_a",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_testlib_loopy_a.cpp"],
@@ -194,7 +201,7 @@
 //
 // this is temporary placeholder - will be removed
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_with_dependency_loop_b_tmp",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_testlib_loopy_invalid.cpp"],
@@ -204,7 +211,7 @@
 // -----------------------------------------------------------------------------
 // libtest_with_dependency_loop_b.so
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_with_dependency_loop_b",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_testlib_loopy_b.cpp"],
@@ -214,7 +221,7 @@
 // -----------------------------------------------------------------------------
 // libtest_with_dependency_loop_c.so
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_with_dependency_loop_c",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_testlib_loopy_c.cpp"],
@@ -230,7 +237,7 @@
 // -----------------------------------------------------------------------------
 
 
-cc_library {
+cc_test_library {
     name: "libtest_relo_check_dt_needed_order",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_testlib_relo_check_dt_needed_order.cpp"],
@@ -240,13 +247,13 @@
     ],
 }
 
-cc_library {
+cc_test_library {
     name: "libtest_relo_check_dt_needed_order_1",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_testlib_relo_check_dt_needed_order_1.cpp"],
 }
 
-cc_library {
+cc_test_library {
     name: "libtest_relo_check_dt_needed_order_2",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_testlib_relo_check_dt_needed_order_2.cpp"],
@@ -260,7 +267,7 @@
 // -----------------------------------------------------------------------------
 // Library used by ifunc tests
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_ifunc",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_testlib_ifunc.c"],
@@ -281,7 +288,7 @@
 // Library used by atexit tests
 // -----------------------------------------------------------------------------
 
-cc_library {
+cc_test_library {
     name: "libtest_atexit",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["atexit_testlib.cpp"],
@@ -291,7 +298,7 @@
 // This library is used by dl_load test to check symbol preempting
 // by main executable
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libdl_preempt_test_1",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dl_preempt_library_1.cpp"],
@@ -301,7 +308,7 @@
 // This library is used by dl_load test to check symbol preempting
 // by libdl_preempt_test_1.so
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libdl_preempt_test_2",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dl_preempt_library_2.cpp"],
@@ -310,7 +317,7 @@
 // -----------------------------------------------------------------------------
 // Library with DF_1_GLOBAL
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libdl_test_df_1_global",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dl_df_1_global.cpp"],
@@ -328,7 +335,7 @@
 // -----------------------------------------------------------------------------
 // Library using symbol from libdl_test_df_1_global
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_dlsym_df_1_global",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dl_df_1_use_global.cpp"],
@@ -337,7 +344,7 @@
 // -----------------------------------------------------------------------------
 // Library with weak function
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_dlsym_weak_func",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlsym_weak_function.cpp"],
@@ -346,7 +353,7 @@
 // -----------------------------------------------------------------------------
 // Library to check RTLD_LOCAL with dlsym in 'this'
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_dlsym_from_this",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlsym_from_this_symbol.cpp"],
@@ -360,7 +367,7 @@
 }
 
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_dlsym_from_this_child",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlsym_from_this_functions.cpp"],
@@ -368,7 +375,7 @@
 }
 
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_dlsym_from_this_grandchild",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlsym_from_this_symbol2.cpp"],
@@ -377,7 +384,7 @@
 // -----------------------------------------------------------------------------
 // Empty library
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_empty",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["empty.cpp"],
@@ -386,7 +393,7 @@
 // -----------------------------------------------------------------------------
 // Library with weak undefined function
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_dlopen_weak_undefined_func",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_weak_undefined.cpp"],
@@ -395,7 +402,7 @@
 // -----------------------------------------------------------------------------
 // Library with constructor that calls dlopen() b/7941716
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_dlopen_from_ctor",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["dlopen_testlib_dlopen_from_ctor.cpp"],
@@ -409,7 +416,7 @@
 // -----------------------------------------------------------------------------
 // Library that depends on the library with constructor that calls dlopen() b/7941716
 // -----------------------------------------------------------------------------
-cc_library {
+cc_test_library {
     name: "libtest_dlopen_from_ctor_main",
     defaults: ["bionic_testlib_defaults"],
     srcs: ["empty.cpp"],
diff --git a/tests/libs/Android.build.dlext_testzip.mk b/tests/libs/Android.build.dlext_testzip.mk
index 56be1e2..37499ba 100644
--- a/tests/libs/Android.build.dlext_testzip.mk
+++ b/tests/libs/Android.build.dlext_testzip.mk
@@ -26,7 +26,7 @@
 LOCAL_MODULE := libdlext_test_zip_zipaligned
 LOCAL_MODULE_SUFFIX := .zip
 LOCAL_MODULE_TAGS := tests
-LOCAL_MODULE_PATH := $($(bionic_2nd_arch_prefix)TARGET_OUT_DATA_NATIVE_TESTS)/libdlext_test_zip
+LOCAL_MODULE_PATH := $($(bionic_2nd_arch_prefix)TARGET_OUT_DATA_NATIVE_TESTS)/bionic-loader-test-libs/libdlext_test_zip
 LOCAL_2ND_ARCH_VAR_PREFIX := $(bionic_2nd_arch_prefix)
 
 include $(BUILD_SYSTEM)/base_rules.mk
@@ -48,7 +48,7 @@
 LOCAL_MODULE := libdlext_test_runpath_zip_zipaligned
 LOCAL_MODULE_SUFFIX := .zip
 LOCAL_MODULE_TAGS := tests
-LOCAL_MODULE_PATH := $($(bionic_2nd_arch_prefix)TARGET_OUT_DATA_NATIVE_TESTS)/libdlext_test_runpath_zip
+LOCAL_MODULE_PATH := $($(bionic_2nd_arch_prefix)TARGET_OUT_DATA_NATIVE_TESTS)/bionic-loader-test-libs/libdlext_test_runpath_zip
 LOCAL_2ND_ARCH_VAR_PREFIX := $(bionic_2nd_arch_prefix)
 
 include $(BUILD_SYSTEM)/base_rules.mk
diff --git a/tests/libs/Android.build.linker_namespaces.mk b/tests/libs/Android.build.linker_namespaces.mk
index f913780..df6428c 100644
--- a/tests/libs/Android.build.linker_namespaces.mk
+++ b/tests/libs/Android.build.linker_namespaces.mk
@@ -35,24 +35,24 @@
 # -----------------------------------------------------------------------------
 libnstest_root_src_files := namespaces_root.cpp
 libnstest_root_shared_libraries := libnstest_public libnstest_private
-libnstest_root_install_to_out_data_dir := private_namespace_libs
+libnstest_root_relative_install_path := private_namespace_libs
 module := libnstest_root
-include $(LOCAL_PATH)/Android.build.target.testlib.mk
+include $(LOCAL_PATH)/Android.build.testlib.target.mk
 
 libnstest_public_src_files := namespaces_public.cpp
 module := libnstest_public
-libnstest_public_install_to_out_data_dir := public_namespace_libs
-include $(LOCAL_PATH)/Android.build.target.testlib.mk
+libnstest_public_relative_install_path := public_namespace_libs
+include $(LOCAL_PATH)/Android.build.testlib.target.mk
 
 libnstest_private_src_files := namespaces_private.cpp
-libnstest_private_install_to_out_data_dir := private_namespace_libs
+libnstest_private_relative_install_path := private_namespace_libs
 module := libnstest_private
-include $(LOCAL_PATH)/Android.build.target.testlib.mk
+include $(LOCAL_PATH)/Android.build.testlib.target.mk
 
 libnstest_dlopened_src_files := namespaces_dlopened.cpp
-libnstest_dlopened_install_to_out_data_dir := private_namespace_libs
+libnstest_dlopened_relative_install_path := private_namespace_libs
 module := libnstest_dlopened
-include $(LOCAL_PATH)/Android.build.target.testlib.mk
+include $(LOCAL_PATH)/Android.build.testlib.target.mk
 
 # -----------------------------------------------------------------------------
 # This set of libraries is to test isolated namespaces
@@ -71,14 +71,14 @@
 # -----------------------------------------------------------------------------
 libnstest_root_not_isolated_src_files := namespaces_root.cpp
 libnstest_root_not_isolated_shared_libraries := libnstest_public libnstest_private_external
-libnstest_root_not_isolated_install_to_out_data_dir := private_namespace_libs
+libnstest_root_not_isolated_relative_install_path := private_namespace_libs
 libnstest_root_not_isolated_ldflags := -Wl,--rpath,\$$ORIGIN/../private_namespace_libs_external \
                                        -Wl,--enable-new-dtags
 
 module := libnstest_root_not_isolated
-include $(LOCAL_PATH)/Android.build.target.testlib.mk
+include $(LOCAL_PATH)/Android.build.testlib.target.mk
 
 libnstest_private_external_src_files := namespaces_private.cpp
-libnstest_private_external_install_to_out_data_dir := private_namespace_libs_external
+libnstest_private_external_relative_install_path := private_namespace_libs_external
 module := libnstest_private_external
-include $(LOCAL_PATH)/Android.build.target.testlib.mk
+include $(LOCAL_PATH)/Android.build.testlib.target.mk
diff --git a/tests/libs/Android.build.testlib.mk b/tests/libs/Android.build.testlib.mk
index 5b688e4..407a389 100644
--- a/tests/libs/Android.build.testlib.mk
+++ b/tests/libs/Android.build.testlib.mk
@@ -17,6 +17,7 @@
 build_target := SHARED_LIBRARY
 build_type := host
 include $(TEST_PATH)/Android.build.mk
-build_type := target
-include $(TEST_PATH)/Android.build.mk
+bionic-loader-test-libs-host: $(LOCAL_MODULE)
+
+include $(LOCAL_PATH)/Android.build.testlib.target.mk
 
diff --git a/tests/libs/Android.build.testlib.target.mk b/tests/libs/Android.build.testlib.target.mk
new file mode 100644
index 0000000..143be5b
--- /dev/null
+++ b/tests/libs/Android.build.testlib.target.mk
@@ -0,0 +1,30 @@
+#
+# Copyright (C) 2014 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.
+#
+
+build_type := target
+# 1. Install test libraries to /data/nativetests../bionic-loader-test-libs/
+#    by default.
+ifeq ($($(module)_relative_install_path),)
+  $(module)_install_to_out_data_dir := bionic-loader-test-libs
+else
+  $(module)_install_to_out_data_dir := bionic-loader-test-libs/$($(module)_relative_install_path)
+endif
+# 2. Set dt_runpath to origin to resolve dependencies
+$(module)_ldflags += -Wl,--rpath,\$${ORIGIN} -Wl,--enable-new-dtags
+include $(TEST_PATH)/Android.build.mk
+
+bionic-loader-test-libs-target: $(LOCAL_MODULE)
+
diff --git a/tests/libs/Android.mk b/tests/libs/Android.mk
index 3c3d78b..94cc516 100644
--- a/tests/libs/Android.mk
+++ b/tests/libs/Android.mk
@@ -29,6 +29,7 @@
     $(LOCAL_PATH)/Android.build.linker_namespaces.mk \
     $(LOCAL_PATH)/Android.build.pthread_atfork.mk \
     $(LOCAL_PATH)/Android.build.testlib.mk \
+    $(LOCAL_PATH)/Android.build.testlib.target.mk \
     $(LOCAL_PATH)/Android.build.versioned_lib.mk \
     $(TEST_PATH)/Android.build.mk
 
@@ -51,12 +52,12 @@
 # create symlink to libdlext_test.so for symlink test
 # -----------------------------------------------------------------------------
 # Use = instead of := to defer the evaluation of $@
-$(TARGET_OUT)/lib/libdlext_test.so: PRIVATE_POST_INSTALL_CMD = \
+$(TARGET_OUT_DATA_NATIVE_TESTS)/bionic-loader-test-libs/libdlext_test.so: PRIVATE_POST_INSTALL_CMD = \
     $(hide) cd $(dir $@) && ln -sf $(notdir $@) libdlext_test_v2.so
 
 ifneq ($(TARGET_2ND_ARCH),)
 # link 64 bit .so
-$(TARGET_OUT)/lib64/libdlext_test.so: PRIVATE_POST_INSTALL_CMD = \
+$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_DATA_NATIVE_TESTS)/bionic-loader-test-libs/libdlext_test.so: PRIVATE_POST_INSTALL_CMD = \
     $(hide) cd $(dir $@) && ln -sf $(notdir $@) libdlext_test_v2.so
 endif
 
@@ -77,11 +78,12 @@
 
 libdlext_test_fd_shared_libraries := libtest_simple
 
-libdlext_test_fd_install_to_out_data_dir := $(module)
+libdlext_test_fd_relative_install_path := $(module)
+
+libdlext_test_fd_ldflags := -Wl,--rpath,\$${ORIGIN}/.. -Wl,--enable-new-dtags
+
 module_tag := optional
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.target.mk
 
 
 # -----------------------------------------------------------------------------
@@ -94,22 +96,18 @@
 
 libdlext_test_zip_shared_libraries := libatest_simple_zip
 
-libdlext_test_zip_install_to_out_data_dir := $(module)
+libdlext_test_zip_relative_install_path := $(module)
 module_tag := optional
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.target.mk
 
 module := libatest_simple_zip
 
 libatest_simple_zip_src_files := \
     dlopen_testlib_simple.cpp
 
-libatest_simple_zip_install_to_out_data_dir := $(module)
+libatest_simple_zip_relative_install_path := $(module)
 module_tag := optional
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.target.mk
 
 # ----------------------------------------------------------------------------
 # Library with soname which does not match filename
diff --git a/tests/utils.h b/tests/utils.h
index 08e8cea..4391f41 100644
--- a/tests/utils.h
+++ b/tests/utils.h
@@ -122,6 +122,9 @@
 // The absolute path to the executable
 const std::string& get_executable_path();
 
+// Get realpath
+bool get_realpath(const std::string& path, std::string* realpath);
+
 // Access to argc/argv/envp
 int get_argc();
 char** get_argv();