Fix symbol lookup order during relocation

  Relocate symbol against DF_1_GLOBAL shared libraries
  loaded before this shared library. This includes
  main executable, ld_preloads and other libraries
  that have DF_1_GLOBAL flag set.

Bug: 2643900
Bug: 15432753
Change-Id: Ia788748be59b739d1c7e62b978e7e255f9e65c7b
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index 2ab3dc1..060f7e0 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -499,6 +499,20 @@
   ASSERT_TRUE(!is_unloaded);
 }
 
+TEST(dlfcn, dlsym_df_1_global) {
+#if !defined(__arm__)
+  void* handle = dlopen("libtest_dlsym_df_1_global.so", RTLD_NOW);
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+  int (*get_answer)();
+  get_answer = reinterpret_cast<int (*)()>(dlsym(handle, "dl_df_1_global_get_answer"));
+  ASSERT_TRUE(get_answer != nullptr) << dlerror();
+  ASSERT_EQ(42, get_answer());
+  ASSERT_EQ(0, dlclose(handle));
+#else
+  GTEST_LOG_(INFO) << "This test does nothing on arm (to be reenabled once b/18137520 or b/18130452 are fixed).\n";
+#endif
+}
+
 TEST(dlfcn, dlopen_failure) {
   void* self = dlopen("/does/not/exist", RTLD_NOW);
   ASSERT_TRUE(self == NULL);