Merge "Remove obsolete -std request."
diff --git a/android-changes-for-ndk-developers.md b/android-changes-for-ndk-developers.md
new file mode 100644
index 0000000..11648fa
--- /dev/null
+++ b/android-changes-for-ndk-developers.md
@@ -0,0 +1,223 @@
+# Android changes for NDK developers
+
+This document details important changes related to native code
+loading in various Android releases.
+
+Required tools: the NDK has an _arch_-linux-android-readelf binary
+(e.g. arm-linux-androideabi-readelf or i686-linux-android-readelf)
+for each architecture (under toolchains/), but you can use readelf for
+any architecture, as we will be doing basic inspection only. On Linux
+you need to have the “binutils” package installed for readelf,
+and “pax-utils” for scanelf.
+
+
+## Private API (Enforced for API level >= 24)
+
+Native libraries must use only public API, and must not link against
+non-NDK platform libraries. Starting with API 24 this rule is enforced and
+applications are no longer able to load non-NDK platform libraries. The
+rule is enforced by the dynamic linker, so non-public libraries
+are not accessible regardless of the way code tries to load them:
+System.loadLibrary, DT_NEEDED entries, and direct calls to dlopen(3)
+will all work exactly the same.
+
+Users should have a consistent app experience across updates,
+and developers shouldn't have to make emergency app updates to
+handle platform changes. For that reason, we recommend against using
+private C/C++ symbols. Private symbols aren't tested as part of the
+Compatibility Test Suite (CTS) that all Android devices must pass. They
+may not exist, or they may behave differently. This makes apps that use
+them more likely to fail on specific devices, or on future releases ---
+as many developers found when Android 6.0 Marshmallow switched from
+OpenSSL to BoringSSL.
+
+In order to reduce the user impact of this transition, we've identified
+a set of libraries that see significant use from Google Play's
+most-installed apps, and that are feasible for us to support in the
+short term (including libandroid_runtime.so, libcutils.so, libcrypto.so,
+and libssl.so). In order to give you more time to transition, we will
+temporarily support these libraries; so if you see a warning that means
+your code will not work in a future release -- please fix it now!
+
+```
+$ readelf --dynamic libBroken.so | grep NEEDED
+ 0x00000001 (NEEDED)                     Shared library: [libnativehelper.so]
+ 0x00000001 (NEEDED)                     Shared library: [libutils.so]
+ 0x00000001 (NEEDED)                     Shared library: [libstagefright_foundation.so]
+ 0x00000001 (NEEDED)                     Shared library: [libmedia_jni.so]
+ 0x00000001 (NEEDED)                     Shared library: [liblog.so]
+ 0x00000001 (NEEDED)                     Shared library: [libdl.so]
+ 0x00000001 (NEEDED)                     Shared library: [libz.so]
+ 0x00000001 (NEEDED)                     Shared library: [libstdc++.so]
+ 0x00000001 (NEEDED)                     Shared library: [libm.so]
+ 0x00000001 (NEEDED)                     Shared library: [libc.so]
+```
+
+*Potential problems*: starting from API 24 the dynamic linker will not
+load private libraries, preventing the application from loading.
+
+*Resolution*: rewrite your native code to rely only on public API. As a
+short term workaround, platform libraries without complex dependencies
+(libcutils.so) can be copied to the project. As a long term solution
+the relevant code must be copied to the project tree. SSL/Media/JNI
+internal/binder APIs should not be accessed from the native code. When
+necessary, native code should call appropriate public Java API methods.
+
+A complete list of public libraries is available within the NDK, under
+platforms/android-API/usr/lib.
+
+Note: SSL/crypto is a special case, applications must NOT use platform
+libcrypto and libssl libraries directly, even on older platforms. All
+applications should use GMS Security Provider to ensure they are protected
+from known vulnerabilities.
+
+
+## Missing Section Headers (Enforced for API level >= 24)
+
+Each ELF file has additional information contained in the section
+headers. These headers must be present now, because the dynamic linker
+uses them for sanity checking. Some developers strip them in an
+attempt to obfuscate the binary and prevent reverse engineering. (This
+doesn't really help because it is possible to reconstruct the stripped
+information using widely-available tools.)
+
+```
+$ readelf --header libBroken.so | grep 'section headers'
+  Start of section headers:          0 (bytes into file)
+  Size of section headers:           0 (bytes)
+  Number of section headers:         0
+```
+
+*Resolution*: remove the extra steps from your build that strip section
+headers.
+
+## Text Relocations (Enforced for API level >= 23)
+
+Starting with API 23, shared objects must not contain text
+relocations. That is, the code must be loaded as is and must not be
+modified. Such an approach reduces load time and improves security.
+
+The usual reason for text relocations is non-position independent
+hand-written assembler. This is not common. Use the scanelf tool as
+described in our documentation for further diagnostics:
+
+```
+$ scanelf -qT libTextRel.so
+  libTextRel.so: (memory/data?) [0x15E0E2] in (optimized out: previous simd_broken_op1) [0x15E0E0]
+  libTextRel.so: (memory/data?) [0x15E3B2] in (optimized out: previous simd_broken_op2) [0x15E3B0]
+  ...
+```
+
+If you have no scanelf tool available, it is possible to do a basic
+check with readelf instead, look for either a TEXTREL entry or the
+TEXTREL flag. Either alone is sufficient. (The value corresponding to the
+TEXTREL entry is irrelevant and typically 0 --- simply the presence of
+the TEXTREL entry declares that the .so contains text relocations). This
+example has both indicators present:
+
+```
+$ readelf --dynamic libTextRel.so | grep TEXTREL
+ 0x00000016 (TEXTREL)                    0x0
+ 0x0000001e (FLAGS)                      SYMBOLIC TEXTREL BIND_NOW
+```
+
+Note: it is technically possible to have a shared object with the TEXTREL
+entry/flag but without any actual text relocations. This doesn't happen
+with the NDK, but if you're generating ELF files yourself make sure
+you're not generating ELF files that claim to have text relocations,
+because the Android dynamic linker trusts the entry/flag.
+
+*Potential problems*: Relocations enforce code pages being writable, and
+wastefully increase the number of dirty pages in memory. The dynamic
+linker has issued warnings about text relocations since Android K
+(API 19), but on API 23 and above it refuses to load code with text
+relocations.
+
+*Resolution*: rewrite assembler to be position independent to ensure
+no text relocations are necessary. The
+[Gentoo Textrels guide](https://wiki.gentoo.org/wiki/Hardened/Textrels_Guide)
+has instructions for fixing text relocations, and more detailed
+[scanelf documentation](https://wiki.gentoo.org/wiki/Hardened/PaX_Utilities).
+
+
+## Invalid DT_NEEDED Entries (Enforced for API level >= 23)
+
+While library dependencies (DT_NEEDED entries in the ELF headers) can be
+absolute paths, that doesn't make sense on Android because you have
+no control over where your library will be installed by the system. A
+DT_NEEDED entry should be the same as the needed library's SONAME,
+leaving the business of finding the library at runtime to the dynamic
+linker.
+
+Before API 23, Android's dynamic linker ignored the full path, and
+used only the basename (the part after the last ‘/') when looking
+up the required libraries. Since API 23 the runtime linker will honor
+the DT_NEEDED exactly and so it won't be able to load the library if
+it is not present in that exact location on the device.
+
+Even worse, some build systems have bugs that cause them to insert
+DT_NEEDED entries that point to a file on the build host, something that
+cannot be found on the device.
+
+```
+$ readelf --dynamic libSample.so | grep NEEDED
+ 0x00000001 (NEEDED)                     Shared library: [libm.so]
+ 0x00000001 (NEEDED)                     Shared library: [libc.so]
+ 0x00000001 (NEEDED)                     Shared library: [libdl.so]
+ 0x00000001 (NEEDED)                     Shared library:
+[C:\Users\build\Android\ci\jni\libBroken.so]
+```
+
+*Potential problems*: before API 23 the DT_NEEDED entry's basename was
+used, but starting from API 23 the Android runtime will try to load the
+library using the path specified, and that path won't exist on the
+device. There are broken third-party toolchains/build systems that use
+a path on a build host instead of the SONAME.
+
+*Resolution*: make sure all required libraries are referenced by SONAME
+only. It is better to let the runtime linker to find and load those
+libraries as the location may change from device to device.
+
+
+## Missing SONAME (Enforced for API level >= 23)
+
+Each ELF shared object (“native library”) must have a SONAME (Shared
+Object Name) attribute. The NDK toolchain adds this attribute by default,
+so its absence indicates either a misconfigured alternative toolchain
+or a misconfiguration in your build system. A missing SONAME may lead
+to runtime issues such as the wrong library being loaded: the filename
+is used instead when this attribute is missing.
+
+```
+$ readelf --dynamic libWithSoName.so | grep SONAME
+ 0x0000000e (SONAME)                     Library soname: [libWithSoName.so]
+```
+
+*Potential problems*: namespace conflicts may lead to the wrong library
+being loaded at runtime, which leads to crashes when required symbols
+are not found, or you try to use an ABI-incompatible library that isn't
+the library you were expecting.
+
+*Resolution*: the current NDK generates the correct SONAME by
+default. Ensure you're using the current NDK and that you haven't
+configured your build system to generate incorrect SONAME entries (using
+the -soname linker option).
+
+
+## Writable and Executable Segments (AOSP master)
+
+Each segment in an ELF file has associated flags that tell the
+dynamic linker what permissions to give the corresponding page in
+memory. For security, data shouldn't be executable and code shouldn't be
+writable. This means that the W (for Writable) and E (for Executable)
+flags should be mutually exclusive. This wasn't historically enforced,
+but is now.
+
+```
+$ readelf --program-headers -W libBadFlags.so | grep WE
+  LOAD           0x000000 0x00000000 0x00000000 0x4c01d 0x4c01d RWE 0x1000
+```
+
+*Resolution*: right now we're not actually sure where these are coming
+from, so if you find and fix these in your app, please let us know how
+they snuck in!
diff --git a/libc/Android.bp b/libc/Android.bp
index c9b2f93..c67e23f 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -2110,4 +2110,10 @@
     first_version: "9",
 }
 
+ndk_library {
+    name: "libstdc++.ndk",
+    symbol_file: "libstdc++.map.txt",
+    first_version: "9",
+}
+
 subdirs = ["malloc_debug"]
diff --git a/libc/bionic/libc_logging.cpp b/libc/bionic/libc_logging.cpp
index 517d047..d87944e 100644
--- a/libc/bionic/libc_logging.cpp
+++ b/libc/bionic/libc_logging.cpp
@@ -432,11 +432,6 @@
 }
 
 static int __libc_write_stderr(const char* tag, const char* msg) {
-  int fd = TEMP_FAILURE_RETRY(open("/dev/stderr", O_CLOEXEC | O_WRONLY | O_APPEND));
-  if (fd == -1) {
-    return -1;
-  }
-
   iovec vec[4];
   vec[0].iov_base = const_cast<char*>(tag);
   vec[0].iov_len = strlen(tag);
@@ -447,8 +442,7 @@
   vec[3].iov_base = const_cast<char*>("\n");
   vec[3].iov_len = 1;
 
-  int result = TEMP_FAILURE_RETRY(writev(fd, vec, 4));
-  close(fd);
+  int result = TEMP_FAILURE_RETRY(writev(STDERR_FILENO, vec, 4));
   return result;
 }
 
diff --git a/libc/libstdc++.map.txt b/libc/libstdc++.map.txt
index 12e26cd..6b8d092 100644
--- a/libc/libstdc++.map.txt
+++ b/libc/libstdc++.map.txt
@@ -1,18 +1,19 @@
 LIBC_O {
   global:
+    _ZSt7nothrow; # var
+    _ZTV9type_info; # var introduced-arm=9 introduced-mips=9
     _ZdaPv;
     _ZdaPvRKSt9nothrow_t;
     _ZdlPv;
     _ZdlPvRKSt9nothrow_t;
-    _Znam; # arm64 x86_64 mips64
-    _ZnamRKSt9nothrow_t; # arm64 x86_64 mips64
-    _Znwm; # arm64 x86_64 mips64
-    _ZnwmRKSt9nothrow_t; # arm64 x86_64 mips64
     _Znaj; # arm x86 mips
     _ZnajRKSt9nothrow_t; # arm x86 mips
+    _Znam; # arm64 x86_64 mips64
+    _ZnamRKSt9nothrow_t; # arm64 x86_64 mips64
     _Znwj; # arm x86 mips
     _ZnwjRKSt9nothrow_t; # arm x86 mips
-    _ZSt7nothrow;
+    _Znwm; # arm64 x86_64 mips64
+    _ZnwmRKSt9nothrow_t; # arm64 x86_64 mips64
     __cxa_guard_abort;
     __cxa_guard_acquire;
     __cxa_guard_release;
diff --git a/tests/Android.bp b/tests/Android.bp
index 4caaa54..3e1e13b 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -250,7 +250,16 @@
 cc_test_library {
     name: "libBionicGtestMain",
     defaults: ["bionic_tests_defaults"],
-    srcs: ["gtest_main.cpp"],
+    srcs: [
+        "gtest_main.cpp",
+        "gtest_globals.cpp",
+    ],
+    static_libs: [
+        "libbase",
+    ],
+    include_dirs: [
+        "bionic/libc",
+    ],
     target: {
         darwin: {
             enabled: true,
@@ -267,6 +276,7 @@
     srcs: [
         "atexit_test.cpp",
         "dl_test.cpp",
+        "dlfcn_symlink_support.cpp",
         "dlfcn_test.cpp",
         "pthread_dlfcn_test.cpp",
     ],
@@ -299,7 +309,10 @@
 cc_test_library {
     name: "libBionicCtsGtestMain",
     defaults: ["bionic_tests_defaults"],
-    srcs: ["gtest_main.cpp"],
+    srcs: [
+        "gtest_main.cpp",
+        "gtest_globals_cts.cpp",
+    ],
     cppflags: ["-DUSING_GTEST_OUTPUT_FORMAT"],
     shared: {
         enabled: false,
@@ -426,6 +439,7 @@
 
     srcs: [
         "atexit_test.cpp",
+        "dlfcn_symlink_support.cpp",
         "dlfcn_test.cpp",
         "dl_test.cpp",
         "pthread_dlfcn_test.cpp",
diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp
index f597e61..8527b00 100644
--- a/tests/dlext_test.cpp
+++ b/tests/dlext_test.cpp
@@ -32,9 +32,11 @@
 #include <pagemap/pagemap.h>
 #include <ziparchive/zip_archive.h>
 
+#include "gtest_globals.h"
 #include "TemporaryFile.h"
 #include "utils.h"
 #include "dlext_private.h"
+#include "dlfcn_symlink_support.h"
 
 #define ASSERT_DL_NOTNULL(ptr) \
     ASSERT_TRUE((ptr) != nullptr) << "dlerror: " << dlerror()
@@ -50,32 +52,22 @@
 
 
 typedef int (*fn)(void);
-#define LIBNAME "libdlext_test.so"
-#define LIBNAME_NORELRO "libdlext_test_norelro.so"
-constexpr auto LIBSIZE = 1024 * 1024; // how much address space to reserve for it
-
-#if defined(__LP64__)
-#define NATIVE_TESTS_PATH "/nativetest64/bionic-loader-test-libs"
-#else
-#define NATIVE_TESTS_PATH "/nativetest/bionic-loader-test-libs"
-#endif
-
-#define LIBPATH NATIVE_TESTS_PATH "/libdlext_test_fd/libdlext_test_fd.so"
-#define LIBZIPPATH NATIVE_TESTS_PATH "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip"
-#define LIBZIPPATH_WITH_RUNPATH NATIVE_TESTS_PATH "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip"
-#define LIBZIP_SIMPLE_ZIP "libdir/libatest_simple_zip.so"
+constexpr const char* kLibName = "libdlext_test.so";
+constexpr const char* kLibNameNoRelro = "libdlext_test_norelro.so";
+constexpr const char* kLibZipSimpleZip = "libdir/libatest_simple_zip.so";
+constexpr auto kLibSize = 1024 * 1024; // how much address space to reserve for it
 
 class DlExtTest : public ::testing::Test {
 protected:
   virtual void SetUp() {
     handle_ = nullptr;
     // verify that we don't have the library loaded already
-    void* h = dlopen(LIBNAME, RTLD_NOW | RTLD_NOLOAD);
+    void* h = dlopen(kLibName, RTLD_NOW | RTLD_NOLOAD);
     ASSERT_TRUE(h == nullptr);
-    h = dlopen(LIBNAME_NORELRO, RTLD_NOW | RTLD_NOLOAD);
+    h = dlopen(kLibNameNoRelro, RTLD_NOW | RTLD_NOLOAD);
     ASSERT_TRUE(h == nullptr);
     // call dlerror() to swallow the error, and check it was the one we wanted
-    ASSERT_STREQ("dlopen failed: library \"" LIBNAME_NORELRO "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
+    ASSERT_EQ(std::string("dlopen failed: library \"") + kLibNameNoRelro + "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
   }
 
   virtual void TearDown() {
@@ -88,7 +80,7 @@
 };
 
 TEST_F(DlExtTest, ExtInfoNull) {
-  handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr);
+  handle_ = android_dlopen_ext(kLibName, RTLD_NOW, nullptr);
   ASSERT_DL_NOTNULL(handle_);
   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
   ASSERT_DL_NOTNULL(f);
@@ -98,7 +90,7 @@
 TEST_F(DlExtTest, ExtInfoNoFlags) {
   android_dlextinfo extinfo;
   extinfo.flags = 0;
-  handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
+  handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
   ASSERT_DL_NOTNULL(handle_);
   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
   ASSERT_DL_NOTNULL(f);
@@ -106,7 +98,7 @@
 }
 
 TEST_F(DlExtTest, ExtInfoUseFd) {
-  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBPATH;
+  const std::string lib_path = g_testlib_root + "/libdlext_test_fd/libdlext_test_fd.so";
 
   android_dlextinfo extinfo;
   extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD;
@@ -124,7 +116,7 @@
 }
 
 TEST_F(DlExtTest, ExtInfoUseFdWithOffset) {
-  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH;
+  const std::string lib_path = g_testlib_root + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
 
   android_dlextinfo extinfo;
   extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
@@ -135,8 +127,8 @@
   ASSERT_EQ(0, OpenArchive(lib_path.c_str(), &handle));
   ZipEntry zip_entry;
   ZipString zip_name;
-  zip_name.name = reinterpret_cast<const uint8_t*>(LIBZIP_SIMPLE_ZIP);
-  zip_name.name_length = sizeof(LIBZIP_SIMPLE_ZIP) - 1;
+  zip_name.name = reinterpret_cast<const uint8_t*>(kLibZipSimpleZip);
+  zip_name.name_length = strlen(kLibZipSimpleZip);
   ASSERT_EQ(0, FindEntry(handle, zip_name, &zip_entry));
   extinfo.library_fd_offset = zip_entry.offset;
   CloseArchive(handle);
@@ -150,9 +142,7 @@
 }
 
 TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) {
-  // lib_path is relative when $ANDROID_DATA is relative
-  std::string lib_path;
-  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")) + LIBZIPPATH, &lib_path)) << strerror(errno);
+  const std::string lib_path = g_testlib_root + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
 
   android_dlextinfo extinfo;
   extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET;
@@ -199,13 +189,15 @@
 }
 
 TEST(dlext, android_dlopen_ext_force_load_smoke) {
+  DlfcnSymlink symlink("android_dlopen_ext_force_load_smoke");
+  const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
   // 1. Open actual file
   void* handle = dlopen("libdlext_test.so", RTLD_NOW);
   ASSERT_DL_NOTNULL(handle);
   // 2. Open link with force_load flag set
   android_dlextinfo extinfo;
   extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
-  void* handle2 = android_dlopen_ext("libdlext_test_v2.so", RTLD_NOW, &extinfo);
+  void* handle2 = android_dlopen_ext(symlink_name.c_str(), RTLD_NOW, &extinfo);
   ASSERT_DL_NOTNULL(handle2);
   ASSERT_TRUE(handle != handle2);
 
@@ -214,15 +206,17 @@
 }
 
 TEST(dlext, android_dlopen_ext_force_load_soname_exception) {
+  DlfcnSymlink symlink("android_dlopen_ext_force_load_soname_exception");
+  const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
   // Check if soname lookup still returns already loaded library
   // when ANDROID_DLEXT_FORCE_LOAD flag is specified.
-  void* handle = dlopen("libdlext_test_v2.so", RTLD_NOW);
+  void* handle = dlopen(symlink_name.c_str(), RTLD_NOW);
   ASSERT_DL_NOTNULL(handle);
 
   android_dlextinfo extinfo;
   extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
 
-  // Note that 'libdlext_test.so' is dt_soname for libdlext_test_v2.so
+  // Note that 'libdlext_test.so' is dt_soname for the symlink_name
   void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo);
 
   ASSERT_DL_NOTNULL(handle2);
@@ -233,8 +227,8 @@
 }
 
 TEST(dlfcn, dlopen_from_zip_absolute_path) {
-  std::string lib_path;
-  ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")) + LIBZIPPATH, &lib_path)) << strerror(errno);
+  const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
+  const std::string lib_path = g_testlib_root + lib_zip_path;
 
   void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW);
   ASSERT_TRUE(handle != nullptr) << dlerror();
@@ -247,9 +241,8 @@
 }
 
 TEST(dlfcn, dlopen_from_zip_with_dt_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;
+  const std::string lib_zip_path = "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip";
+  const std::string lib_path = g_testlib_root + lib_zip_path;
 
   void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW);
 
@@ -267,9 +260,8 @@
 }
 
 TEST(dlfcn, dlopen_from_zip_ld_library_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 + LIBZIPPATH + "!/libdir";
+  const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip";
+  const std::string lib_path = g_testlib_root + lib_zip_path + "!/libdir";
 
   typedef void (*fn_t)(const char*);
   fn_t android_update_LD_LIBRARY_PATH =
@@ -300,19 +292,19 @@
 
 
 TEST_F(DlExtTest, Reserved) {
-  void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
   ASSERT_TRUE(start != MAP_FAILED);
   android_dlextinfo extinfo;
   extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
   extinfo.reserved_addr = start;
-  extinfo.reserved_size = LIBSIZE;
-  handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
+  extinfo.reserved_size = kLibSize;
+  handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
   ASSERT_DL_NOTNULL(handle_);
   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
   ASSERT_DL_NOTNULL(f);
   EXPECT_GE(reinterpret_cast<void*>(f), start);
   EXPECT_LT(reinterpret_cast<void*>(f),
-            reinterpret_cast<char*>(start) + LIBSIZE);
+            reinterpret_cast<char*>(start) + kLibSize);
   EXPECT_EQ(4, f());
 
   // Check that after dlclose reserved address space is unmapped (and can be reused)
@@ -330,24 +322,24 @@
   extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
   extinfo.reserved_addr = start;
   extinfo.reserved_size = PAGE_SIZE;
-  handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
+  handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
   EXPECT_EQ(nullptr, handle_);
 }
 
 TEST_F(DlExtTest, ReservedHint) {
-  void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
   ASSERT_TRUE(start != MAP_FAILED);
   android_dlextinfo extinfo;
   extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
   extinfo.reserved_addr = start;
-  extinfo.reserved_size = LIBSIZE;
-  handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
+  extinfo.reserved_size = kLibSize;
+  handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
   ASSERT_DL_NOTNULL(handle_);
   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
   ASSERT_DL_NOTNULL(f);
   EXPECT_GE(reinterpret_cast<void*>(f), start);
   EXPECT_LT(reinterpret_cast<void*>(f),
-            reinterpret_cast<char*>(start) + LIBSIZE);
+            reinterpret_cast<char*>(start) + kLibSize);
   EXPECT_EQ(4, f());
 }
 
@@ -358,7 +350,7 @@
   extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT;
   extinfo.reserved_addr = start;
   extinfo.reserved_size = PAGE_SIZE;
-  handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
+  handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
   ASSERT_DL_NOTNULL(handle_);
   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
   ASSERT_DL_NOTNULL(f);
@@ -369,36 +361,36 @@
 }
 
 TEST_F(DlExtTest, LoadAtFixedAddress) {
-  void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
   ASSERT_TRUE(start != MAP_FAILED);
-  munmap(start, LIBSIZE);
+  munmap(start, kLibSize);
 
   android_dlextinfo extinfo;
   extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
   extinfo.reserved_addr = start;
 
-  handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
+  handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
   ASSERT_DL_NOTNULL(handle_);
   fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
   ASSERT_DL_NOTNULL(f);
   EXPECT_GE(reinterpret_cast<void*>(f), start);
-  EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + LIBSIZE);
+  EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + kLibSize);
 
   EXPECT_EQ(4, f());
   dlclose(handle_);
   handle_ = nullptr;
 
   // Check that dlclose unmapped the file
-  void* addr = mmap(start, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  void* addr = mmap(start, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
   ASSERT_EQ(start, addr) << "dlclose did not unmap the memory";
 }
 
 TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) {
-  void* start = mmap(nullptr, LIBSIZE + PAGE_SIZE, PROT_NONE,
+  void* start = mmap(nullptr, kLibSize + PAGE_SIZE, PROT_NONE,
                          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
   ASSERT_TRUE(start != MAP_FAILED);
-  munmap(start, LIBSIZE + PAGE_SIZE);
-  void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, LIBSIZE, PROT_NONE,
+  munmap(start, kLibSize + PAGE_SIZE);
+  void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, kLibSize, PROT_NONE,
                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
   ASSERT_TRUE(new_addr != MAP_FAILED);
 
@@ -406,7 +398,7 @@
   extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS;
   extinfo.reserved_addr = start;
 
-  handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo);
+  handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo);
   ASSERT_TRUE(handle_ == nullptr);
 }
 
@@ -414,11 +406,11 @@
 protected:
   virtual void SetUp() {
     DlExtTest::SetUp();
-    void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+    void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
     ASSERT_TRUE(start != MAP_FAILED);
     extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS;
     extinfo_.reserved_addr = start;
-    extinfo_.reserved_size = LIBSIZE;
+    extinfo_.reserved_size = kLibSize;
     extinfo_.relro_fd = -1;
   }
 
@@ -477,8 +469,8 @@
   TemporaryFile tf; // Use tf to get an unique filename.
   ASSERT_NOERROR(close(tf.fd));
 
-  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
-  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
+  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename));
+  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
 
   // Use destructor of tf to close and unlink the file.
   tf.fd = extinfo_.relro_fd;
@@ -488,15 +480,15 @@
   TemporaryFile tf; // // Use tf to get an unique filename.
   ASSERT_NOERROR(close(tf.fd));
 
-  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename));
-  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO));
+  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameNoRelro, tf.filename));
+  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibNameNoRelro));
 
   // Use destructor of tf to close and unlink the file.
   tf.fd = extinfo_.relro_fd;
 }
 
 TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
-  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME));
+  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
 }
 
 TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
@@ -508,14 +500,14 @@
   TemporaryFile tf; // Use tf to get an unique filename.
   ASSERT_NOERROR(close(tf.fd));
 
-  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename));
+  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename));
 
   int pipefd[2];
   ASSERT_NOERROR(pipe(pipefd));
 
   size_t without_sharing, with_sharing;
-  ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing));
-  ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing));
+  ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, false, &without_sharing));
+  ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, true, &with_sharing));
 
   // We expect the sharing to save at least 10% of the total PSS. In practice
   // it saves 40%+ for this test.
@@ -639,9 +631,7 @@
   ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: "
                "the list of public libraries is empty.", dlerror());
 
-  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
-
-  const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
+  const std::string lib_public_path = g_testlib_root + "/public_namespace_libs/" + g_public_lib;
   void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle_public != nullptr) << dlerror();
 
@@ -649,20 +639,20 @@
 
   // Check that libraries added to public namespace are NODELETE
   dlclose(handle_public);
-  handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(),
+  handle_public = dlopen((g_testlib_root + "/public_namespace_libs/" + g_public_lib).c_str(),
                          RTLD_NOW | RTLD_NOLOAD);
 
   ASSERT_TRUE(handle_public != nullptr) << dlerror();
 
   android_namespace_t* ns1 =
           android_create_namespace("private", nullptr,
-                                   (lib_path + "/private_namespace_libs").c_str(),
+                                   (g_testlib_root + "/private_namespace_libs").c_str(),
                                    ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
   ASSERT_TRUE(ns1 != nullptr) << dlerror();
 
   android_namespace_t* ns2 =
           android_create_namespace("private_isolated", nullptr,
-                                   (lib_path + "/private_namespace_libs").c_str(),
+                                   (g_testlib_root + "/private_namespace_libs").c_str(),
                                    ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr, nullptr);
   ASSERT_TRUE(ns2 != nullptr) << dlerror();
 
@@ -753,8 +743,7 @@
   static const char* root_lib = "libnstest_root_not_isolated.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;
-  const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
+  const std::string lib_public_path = g_testlib_root + "/public_namespace_libs/" + g_public_lib;
   void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle_public != nullptr) << dlerror();
 
@@ -764,14 +753,14 @@
 
   android_namespace_t* ns_not_isolated =
           android_create_namespace("private", nullptr,
-                                   (lib_path + "/private_namespace_libs").c_str(),
+                                   (g_testlib_root + "/private_namespace_libs").c_str(),
                                    ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
   ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
 
   android_namespace_t* ns_isolated =
           android_create_namespace("private_isolated1",
                                    nullptr,
-                                   (lib_path + "/private_namespace_libs").c_str(),
+                                   (g_testlib_root + "/private_namespace_libs").c_str(),
                                    ANDROID_NAMESPACE_TYPE_ISOLATED,
                                    nullptr,
                                    nullptr);
@@ -779,10 +768,10 @@
 
   android_namespace_t* ns_isolated2 =
           android_create_namespace("private_isolated2",
-                                   (lib_path + "/private_namespace_libs").c_str(),
+                                   (g_testlib_root + "/private_namespace_libs").c_str(),
                                    nullptr,
                                    ANDROID_NAMESPACE_TYPE_ISOLATED,
-                                   lib_path.c_str(),
+                                   g_testlib_root.c_str(),
                                    nullptr);
   ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror();
 
@@ -790,7 +779,7 @@
   ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
 
   std::string lib_private_external_path =
-      lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
+      g_testlib_root + "/private_namespace_libs_external/libnstest_private_external.so";
 
   // Load lib_private_external_path to default namespace
   // (it should remain invisible for the isolated namespaces after this)
@@ -819,7 +808,7 @@
 
   extinfo.library_namespace = ns_isolated2;
 
-  // this should work because isolation_path for private_isolated2 includes lib_path
+  // this should work because isolation_path for private_isolated2 includes g_testlib_root
   handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo);
   ASSERT_TRUE(handle2 != nullptr) << dlerror();
   dlclose(handle2);
@@ -860,8 +849,7 @@
   static const char* root_lib_isolated = "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;
-  const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
+  const std::string lib_public_path = g_testlib_root + "/public_namespace_libs/" + g_public_lib;
   void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle_public != nullptr) << dlerror();
 
@@ -872,18 +860,18 @@
   // preload this library to the default namespace to check if it
   // is shared later on.
   void* handle_dlopened =
-          dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
+          dlopen((g_testlib_root + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
   ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
 
   android_namespace_t* ns_not_isolated =
           android_create_namespace("private", nullptr,
-                                   (lib_path + "/private_namespace_libs").c_str(),
+                                   (g_testlib_root + "/private_namespace_libs").c_str(),
                                    ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
   ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror();
 
   android_namespace_t* ns_isolated_shared =
           android_create_namespace("private_isolated_shared", nullptr,
-                                   (lib_path + "/private_namespace_libs").c_str(),
+                                   (g_testlib_root + "/private_namespace_libs").c_str(),
                                    ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
                                    nullptr, nullptr);
   ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
@@ -892,7 +880,7 @@
   ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror());
 
   std::string lib_private_external_path =
-      lib_path + "/private_namespace_libs_external/libnstest_private_external.so";
+      g_testlib_root + "/private_namespace_libs_external/libnstest_private_external.so";
 
   // Load lib_private_external_path to default namespace
   // (it should remain invisible for the isolated namespaces after this)
@@ -976,8 +964,6 @@
 TEST(dlext, ns_shared_dlclose) {
   std::string path = "libc.so:libc++.so:libdl.so:libm.so";
 
-  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
-
   android_set_application_target_sdk_version(42U); // something > 23
 
   ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror();
@@ -985,12 +971,12 @@
   // preload this library to the default namespace to check if it
   // is shared later on.
   void* handle_dlopened =
-          dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
+          dlopen((g_testlib_root + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW);
   ASSERT_TRUE(handle_dlopened != nullptr) << dlerror();
 
   android_namespace_t* ns_isolated_shared =
           android_create_namespace("private_isolated_shared", nullptr,
-                                   (lib_path + "/private_namespace_libs").c_str(),
+                                   (g_testlib_root + "/private_namespace_libs").c_str(),
                                    ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED,
                                    nullptr, nullptr);
   ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror();
@@ -1011,7 +997,7 @@
   ASSERT_TRUE(handle == nullptr)
       << "Error: libnstest_dlopened.so is still accessible in shared namespace";
 
-  handle = android_dlopen_ext((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
+  handle = android_dlopen_ext((g_testlib_root + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
                               RTLD_NOW | RTLD_NOLOAD, &extinfo);
   ASSERT_TRUE(handle == nullptr)
       << "Error: libnstest_dlopened.so is still accessible in shared namespace";
@@ -1020,14 +1006,14 @@
   ASSERT_TRUE(handle == nullptr)
       << "Error: libnstest_dlopened.so is still accessible in default namespace";
 
-  handle = dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
+  handle = dlopen((g_testlib_root + "/private_namespace_libs/libnstest_dlopened.so").c_str(),
                   RTLD_NOW | RTLD_NOLOAD);
   ASSERT_TRUE(handle == nullptr)
       << "Error: libnstest_dlopened.so is still accessible in default namespace";
 
   // Now lets see if the soinfo area gets reused in the wrong way:
   // load a library to default namespace.
-  const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib;
+  const std::string lib_public_path = g_testlib_root + "/public_namespace_libs/" + g_public_lib;
   void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle_public != nullptr) << dlerror();
 
@@ -1043,14 +1029,12 @@
 
   ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr));
 
-  const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH;
-
-  const std::string lib_public_path = lib_path + "/public_namespace_libs";
+  const std::string lib_public_path = g_testlib_root + "/public_namespace_libs";
 
   android_namespace_t* ns1 =
           android_create_namespace("isolated1",
                                    nullptr,
-                                   (lib_path + "/private_namespace_libs").c_str(),
+                                   (g_testlib_root + "/private_namespace_libs").c_str(),
                                    ANDROID_NAMESPACE_TYPE_ISOLATED,
                                    lib_public_path.c_str(),
                                    nullptr);
@@ -1059,7 +1043,7 @@
   android_namespace_t* ns2 =
           android_create_namespace("isolated2",
                                    nullptr,
-                                   (lib_path + "/private_namespace_libs").c_str(),
+                                   (g_testlib_root + "/private_namespace_libs").c_str(),
                                    ANDROID_NAMESPACE_TYPE_ISOLATED,
                                    lib_public_path.c_str(),
                                    nullptr);
@@ -1078,7 +1062,7 @@
   android_namespace_t* ns1_child =
         android_create_namespace("isolated1_child",
                                  nullptr,
-                                 (lib_path + "/private_namespace_libs").c_str(),
+                                 (g_testlib_root + "/private_namespace_libs").c_str(),
                                  ANDROID_NAMESPACE_TYPE_ISOLATED,
                                  nullptr,
                                  ns1);
@@ -1113,26 +1097,22 @@
   static const char* root_lib = "libnstest_root.so";
   std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib;
 
-  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;
+  const std::string lib_public_path = g_testlib_root + "/public_namespace_libs/" + g_public_lib;
   void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW);
 
   ASSERT_TRUE(handle_public != nullptr) << dlerror();
 
-  ASSERT_TRUE(android_init_namespaces(path.c_str(), (lib_path + "/private_namespace_libs").c_str()))
+  ASSERT_TRUE(android_init_namespaces(path.c_str(), (g_testlib_root + "/private_namespace_libs").c_str()))
       << dlerror();
 
   android_namespace_t* ns = android_create_namespace(
                                 "private", nullptr,
-                                (lib_path + "/private_namespace_libs").c_str(),
+                                (g_testlib_root + "/private_namespace_libs").c_str(),
                                 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr);
 
   ASSERT_TRUE(ns != nullptr) << dlerror();
 
-  std::string private_library_absolute_path = lib_path + "/private_namespace_libs/" + root_lib;
+  std::string private_library_absolute_path = g_testlib_root + "/private_namespace_libs/" + root_lib;
 
   android_dlextinfo extinfo;
   extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
diff --git a/tests/dlfcn_symlink_support.cpp b/tests/dlfcn_symlink_support.cpp
new file mode 100644
index 0000000..be1839e
--- /dev/null
+++ b/tests/dlfcn_symlink_support.cpp
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2016 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 "dlfcn_symlink_support.h"
+
+#include <gtest/gtest.h>
+
+#include <dlfcn.h>
+#include <libgen.h>
+#include <link.h>
+#include <unistd.h>
+
+#include <android-base/strings.h>
+
+#include <algorithm>
+#include <string>
+#include <vector>
+
+static const constexpr char* source_file_name = "libdlext_test.so";
+static const constexpr char* symlink_name_prefix = "libdlext_test_";
+
+static int dl_callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
+  // The case when path is not absolute and is equal to source_file_name
+  // is disregarded intentionally since in bionic dlpi_name should always
+  // be realpath to a shared object.
+  const std::string suffix = std::string("/") + source_file_name;
+
+  // TODO (dimitry): remove this check once fake libdl.so is gone
+  if (info->dlpi_name == nullptr) {
+    // This is linker imposing as libdl.so - skip it
+    return 0;
+  }
+
+  if (android::base::EndsWith(info->dlpi_name, suffix.c_str())) {
+    std::string* path = reinterpret_cast<std::string*>(data);
+    *path = info->dlpi_name;
+    return 1; // found
+  }
+
+  return 0;
+}
+
+void create_dlfcn_test_symlink(const char* suffix, std::string* result) {
+  void* handle = dlopen(source_file_name, RTLD_NOW);
+  std::string source_file_path;
+
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+  ASSERT_TRUE(dl_iterate_phdr(dl_callback, &source_file_path) == 1)
+      << "dl_phdr_info for \"" << source_file_name << "\" was not found.";
+
+  dlclose(handle);
+  std::vector<char> buf;
+  std::copy(source_file_path.begin(), source_file_path.end(), std::back_inserter(buf));
+  buf.push_back('\0');
+
+  std::string path_dir = dirname(&buf[0]);
+  std::string link_path = path_dir + "/" + symlink_name_prefix + suffix + ".so";
+
+  ASSERT_TRUE(symlink(source_file_path.c_str(), link_path.c_str()) == 0) << strerror(errno);
+  *result = link_path;
+}
+
+void remove_dlfcn_test_symlink(const std::string& path) {
+  ASSERT_TRUE(unlink(path.c_str()) == 0) << strerror(errno);
+}
diff --git a/tests/dlfcn_symlink_support.h b/tests/dlfcn_symlink_support.h
new file mode 100644
index 0000000..8a8d3a2
--- /dev/null
+++ b/tests/dlfcn_symlink_support.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#ifndef __DLFCN_SYMLINK_SUPPORT_H__
+#define __DLFCN_SYMLINK_SUPPORT_H__
+
+#include <string>
+
+void create_dlfcn_test_symlink(const char* suffix, std::string* result);
+void remove_dlfcn_test_symlink(const std::string& path);
+
+class DlfcnSymlink {
+ public:
+  explicit DlfcnSymlink(const char* test_name) {
+    create_dlfcn_test_symlink(test_name, &symlink_path_);
+  }
+
+  ~DlfcnSymlink() {
+    remove_dlfcn_test_symlink(symlink_path_);
+  }
+
+  const std::string& get_symlink_path() const {
+    return symlink_path_;
+  }
+
+ private:
+  std::string symlink_path_;
+};
+
+#endif /* __DLFCN_SYMLINK_SUPPORT_H__ */
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index 34c4108..dd9660b 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -17,15 +17,17 @@
 #include <gtest/gtest.h>
 
 #include <dlfcn.h>
-#include <libgen.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <string.h>
 
 #include "private/ScopeGuard.h"
 
 #include <string>
 
+#include "gtest_globals.h"
+#include "dlfcn_symlink_support.h"
 #include "utils.h"
 
 #define ASSERT_SUBSTR(needle, haystack) \
@@ -1012,8 +1014,10 @@
 }
 
 TEST(dlfcn, dlopen_symlink) {
+  DlfcnSymlink symlink("dlopen_symlink");
+  const std::string symlink_name = basename(symlink.get_symlink_path().c_str());
   void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
-  void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
+  void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle1 != nullptr);
   ASSERT_TRUE(handle2 != nullptr);
   ASSERT_EQ(handle1, handle2);
@@ -1132,16 +1136,8 @@
 // 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) {
-  std::string libpath = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH + "/libtest_dt_runpath_d.so";
+  std::string libpath = g_testlib_root + "/libtest_dt_runpath_d.so";
   void* handle = dlopen(libpath.c_str(), RTLD_NOW);
   ASSERT_TRUE(handle != nullptr) << dlerror();
 
@@ -1156,9 +1152,9 @@
 }
 
 TEST(dlfcn, dlopen_invalid_rw_load_segment) {
-  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";
+  const std::string libpath = g_testlib_root +
+                              "/" + kPrebuiltElfDir +
+                              "/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";
@@ -1166,9 +1162,10 @@
 }
 
 TEST(dlfcn, dlopen_invalid_unaligned_shdr_offset) {
-  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";
+  const std::string libpath = g_testlib_root +
+                              "/" + kPrebuiltElfDir +
+                              "/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: ";
@@ -1176,9 +1173,10 @@
 }
 
 TEST(dlfcn, dlopen_invalid_zero_shentsize) {
-  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";
+  const std::string libpath = g_testlib_root +
+                              "/" + kPrebuiltElfDir +
+                              "/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";
@@ -1186,9 +1184,10 @@
 }
 
 TEST(dlfcn, dlopen_invalid_zero_shstrndx) {
-  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";
+  const std::string libpath = g_testlib_root +
+                              "/" + kPrebuiltElfDir +
+                              "/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";
@@ -1196,9 +1195,10 @@
 }
 
 TEST(dlfcn, dlopen_invalid_empty_shdr_table) {
-  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";
+  const std::string libpath = g_testlib_root +
+                              "/" + kPrebuiltElfDir +
+                              "/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";
@@ -1206,9 +1206,10 @@
 }
 
 TEST(dlfcn, dlopen_invalid_zero_shdr_table_offset) {
-  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";
+  const std::string libpath = g_testlib_root +
+                              "/" + kPrebuiltElfDir +
+                              "/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/";
@@ -1216,9 +1217,10 @@
 }
 
 TEST(dlfcn, dlopen_invalid_zero_shdr_table_content) {
-  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";
+  const std::string libpath = g_testlib_root +
+                              "/" + kPrebuiltElfDir +
+                              "/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_globals.cpp b/tests/gtest_globals.cpp
new file mode 100644
index 0000000..4f2c82e
--- /dev/null
+++ b/tests/gtest_globals.cpp
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2016 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 "gtest_globals.h"
+
+#include <gtest/gtest.h>
+#include "utils.h"
+
+#include <string>
+
+static std::string get_testlib_root() {
+  std::string out_path;
+  const char* data_dir = getenv("ANDROID_DATA");
+  if (data_dir == nullptr) {
+    out_path = "/data";
+  } else {
+    out_path = data_dir;
+  }
+
+  out_path = out_path + "/nativetest";
+#if defined(__LP64__)
+  out_path += "64";
+#endif
+  out_path += "/bionic-loader-test-libs";
+  std::string real_path;
+  if (!get_realpath(out_path, &real_path)) {
+    abort();
+  }
+
+  return real_path;
+}
+
+const std::string g_testlib_root = get_testlib_root();
diff --git a/tests/gtest_globals.h b/tests/gtest_globals.h
new file mode 100644
index 0000000..fab2a39
--- /dev/null
+++ b/tests/gtest_globals.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#ifndef _BIONIC_TESTS_GTEST_GLOBALS_H
+#define _BIONIC_TESTS_GTEST_GLOBALS_H
+
+#include <string>
+
+constexpr const char* kPrebuiltElfDir = "prebuilt-elf-files";
+
+extern const std::string g_testlib_root;
+
+#endif  // _BIONIC_TESTS_GTEST_GLOBALS_H
diff --git a/tests/gtest_globals_cts.cpp b/tests/gtest_globals_cts.cpp
new file mode 100644
index 0000000..bf891a1
--- /dev/null
+++ b/tests/gtest_globals_cts.cpp
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2016 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 "gtest_globals.h"
+
+#include <string>
+
+const std::string g_testlib_root = "/data/local/tmp/lib/bionic-loader-test-libs";
diff --git a/tests/libs/Android.mk b/tests/libs/Android.mk
index 94cc516..a54318d 100644
--- a/tests/libs/Android.mk
+++ b/tests/libs/Android.mk
@@ -49,26 +49,6 @@
 include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
-# create symlink to libdlext_test.so for symlink test
-# -----------------------------------------------------------------------------
-# Use = instead of := to defer the evaluation of $@
-$(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_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
-
-# host symlinks
-$(HOST_OUT)/lib64/libdlext_test.so: PRIVATE_POST_INSTALL_CMD = \
-    $(hide) cd $(dir $@) && ln -sf $(notdir $@) libdlext_test_v2.so
-
-$(HOST_OUT)/lib/libdlext_test.so: PRIVATE_POST_INSTALL_CMD = \
-    $(hide) cd $(dir $@) && ln -sf $(notdir $@) libdlext_test_v2.so
-
-# -----------------------------------------------------------------------------
 # Library used by dlext tests - different name non-default location
 # -----------------------------------------------------------------------------
 module := libdlext_test_fd
diff --git a/tests/utils.h b/tests/utils.h
index 4391f41..c62da75 100644
--- a/tests/utils.h
+++ b/tests/utils.h
@@ -32,6 +32,8 @@
 
 #include "private/ScopeGuard.h"
 
+#if defined(__linux__)
+
 struct map_record {
   uintptr_t addr_start;
   uintptr_t addr_end;
@@ -95,6 +97,8 @@
 
 extern "C" pid_t gettid();
 
+#endif
+
 static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
   while (tid == 0) {
     usleep(1000);