Implement lookup by DT_SONAME

  This CL also fixes SEARCH_NAME hack and resolves
  https://code.google.com/p/android/issues/detail?id=6670
  once and for all.

Bug: https://code.google.com/p/android/issues/detail?id=6670
Change-Id: I9b8d6a672cd722f30fbfbb40cdee8d9b39cfe56e
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index ef4ff20..2bd9476 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -97,6 +97,32 @@
   ASSERT_EQ(0, dlclose(handle2));
 }
 
+TEST(dlfcn, dlopen_by_soname) {
+  static const char* soname = "libdlext_test_soname.so";
+  static const char* filename = "libdlext_test_different_soname.so";
+  // 1. Make sure there is no library with soname in default search path
+  void* handle = dlopen(soname, RTLD_NOW);
+  ASSERT_TRUE(handle == nullptr);
+
+  // 2. Load a library using filename
+  handle = dlopen(filename, RTLD_NOW);
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+
+  // 3. Find library by soname
+  void* handle_soname = dlopen(soname, RTLD_NOW | RTLD_NOLOAD);
+  ASSERT_TRUE(handle_soname != nullptr) << dlerror();
+  ASSERT_EQ(handle, handle_soname);
+
+  // 4. RTLD_NOLOAD should still work with filename
+  void* handle_filename = dlopen(filename, RTLD_NOW | RTLD_NOLOAD);
+  ASSERT_TRUE(handle_filename != nullptr) << dlerror();
+  ASSERT_EQ(handle, handle_filename);
+
+  dlclose(handle_filename);
+  dlclose(handle_soname);
+  dlclose(handle);
+}
+
 // ifuncs are only supported on intel and arm64 for now
 #if defined (__aarch64__) || defined(__i386__) || defined(__x86_64__)
 TEST(dlfcn, ifunc) {