Fix relocation to look for symbols in local group

  The local group is a sequence of libraries in default (breadth-first)
  order. It allows RTLD_LOCALLY loaded library to correctly relocate
  symbols within its group (see test-cases).

  Local group lookup is performed after main executable and ld_preloads.

Bug: 2643900
Bug: 15432753
Bug: 18186310

(cherry picked from commit cfa97f172dc1b10d650fefbb6ccffd88ce72a5fb)

Change-Id: I5fa8c673f929e4652c738912c7ae078d7ec286d2
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index f1ec0f1..e604f5a 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -162,39 +162,39 @@
   ASSERT_EQ(1, fn());
 }
 
-TEST(dlfcn, dlopen_check_order) {
+TEST(dlfcn, dlopen_check_order_dlsym) {
   // Here is how the test library and its dt_needed
   // libraries are arranged
   //
-  //  libtest_check_order.so
+  //  libtest_check_order_children.so
   //  |
-  //  +-> libtest_check_order_1_left.so
+  //  +-> ..._1_left.so
   //  |   |
-  //  |   +-> libtest_check_order_a.so
+  //  |   +-> ..._a.so
   //  |   |
-  //  |   +-> libtest_check_order_b.so
+  //  |   +-> ...r_b.so
   //  |
-  //  +-> libtest_check_order_2_right.so
+  //  +-> ..._2_right.so
   //  |   |
-  //  |   +-> libtest_check_order_d.so
+  //  |   +-> ..._d.so
   //  |       |
-  //  |       +-> libtest_check_order_b.so
+  //  |       +-> ..._b.so
   //  |
-  //  +-> libtest_check_order_3_c.so
+  //  +-> ..._3_c.so
   //
   //  load order should be (1, 2, 3, a, b, d)
   //
   // get_answer() is defined in (2, 3, a, b, c)
   // get_answer2() is defined in (b, d)
-  void* sym = dlsym(RTLD_DEFAULT, "dlopen_test_get_answer");
+  void* sym = dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer");
   ASSERT_TRUE(sym == nullptr);
-  void* handle = dlopen("libtest_check_order.so", RTLD_NOW | RTLD_GLOBAL);
-  ASSERT_TRUE(handle != nullptr);
+  void* handle = dlopen("libtest_check_order_dlsym.so", RTLD_NOW | RTLD_GLOBAL);
+  ASSERT_TRUE(handle != nullptr) << dlerror();
   typedef int (*fn_t) (void);
   fn_t fn, fn2;
-  fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "dlopen_test_get_answer"));
+  fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer"));
   ASSERT_TRUE(fn != NULL) << dlerror();
-  fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "dlopen_test_get_answer2"));
+  fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "check_order_dlsym_get_answer2"));
   ASSERT_TRUE(fn2 != NULL) << dlerror();
 
   ASSERT_EQ(42, fn());
@@ -202,6 +202,163 @@
   dlclose(handle);
 }
 
+TEST(dlfcn, dlopen_check_order_reloc_siblings) {
+  // This is how this one works:
+  // we lookup and call get_answer which is defined in '_2.so'
+  // and in turn calls external get_answer_impl() defined in _1.so and in '_[a-f].so'
+  // the correct _impl() is implemented by '_a.so';
+  //
+  // Note that this is test for RTLD_LOCAL (TODO: test for GLOBAL?)
+  //
+  // Here is the picture:
+  //
+  // libtest_check_order_reloc_siblings.so
+  // |
+  // +-> ..._1.so <- empty
+  // |   |
+  // |   +-> ..._a.so <- exports correct answer_impl()
+  // |   |
+  // |   +-> ..._b.so <- every other letter exporting incorrect one.
+  // |
+  // +-> ..._2.so <- empty
+  // |   |
+  // |   +-> ..._c.so
+  // |   |
+  // |   +-> ..._d.so
+  // |
+  // +-> ..._3.so <- empty
+  //     |
+  //     +-> ..._e.so
+  //     |
+  //     +-> ..._f.so <- exports get_answer() that calls get_anser_impl();
+  //                     implements incorrect get_answer_impl()
+
+  void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
+  ASSERT_TRUE(handle == nullptr);
+#ifdef __BIONIC__
+  // TODO: glibc returns nullptr on dlerror() here. Is it bug?
+  ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
+#endif
+
+  handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+
+  typedef int (*fn_t) (void);
+  fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
+  ASSERT_TRUE(fn != nullptr) << dlerror();
+  ASSERT_EQ(42, fn());
+
+  ASSERT_EQ(0, dlclose(handle));
+}
+
+TEST(dlfcn, dlopen_check_order_reloc_siblings_with_preload) {
+  // This test uses the same library as dlopen_check_order_reloc_siblings.
+  // Unlike dlopen_check_order_reloc_siblings it preloads
+  // libtest_check_order_reloc_siblings_1.so (first dependency) prior to
+  // dlopen(libtest_check_order_reloc_siblings.so)
+
+  void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
+  ASSERT_TRUE(handle == nullptr);
+  handle = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_NOLOAD);
+  ASSERT_TRUE(handle == nullptr);
+
+  void* handle_for_1 = dlopen("libtest_check_order_reloc_siblings_1.so", RTLD_NOW | RTLD_LOCAL);
+  ASSERT_TRUE(handle_for_1 != nullptr) << dlerror();
+
+  handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+
+  ASSERT_EQ(0, dlclose(handle_for_1));
+
+  typedef int (*fn_t) (void);
+  fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_get_answer"));
+  ASSERT_TRUE(fn != nullptr) << dlerror();
+  ASSERT_EQ(42, fn());
+
+  ASSERT_EQ(0, dlclose(handle));
+}
+
+TEST(dlfcn, dlopen_check_order_reloc_nephew) {
+  // This is how this one works:
+  // we lookup and call nephew_get_answer which is defined in '_2.so'
+  // and in turn calls external get_answer_impl() defined in '_[a-f].so'
+  // the correct _impl() is implemented by '_a.so';
+  //
+  // Here is the picture:
+  //
+  // libtest_check_order_reloc_siblings.so
+  // |
+  // +-> ..._1.so <- empty
+  // |   |
+  // |   +-> ..._a.so <- exports correct answer_impl()
+  // |   |
+  // |   +-> ..._b.so <- every other letter exporting incorrect one.
+  // |
+  // +-> ..._2.so <- empty
+  // |   |
+  // |   +-> ..._c.so
+  // |   |
+  // |   +-> ..._d.so
+  // |
+  // +-> ..._3.so <- nephew_get_answer() that calls get_answer_impl();
+  //     |
+  //     +-> ..._e.so
+  //     |
+  //     +-> ..._f.so
+
+  void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
+  ASSERT_TRUE(handle == nullptr);
+#ifdef __BIONIC__
+  // TODO: glibc returns nullptr on dlerror() here. Is it bug?
+  ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
+#endif
+
+  handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+
+  typedef int (*fn_t) (void);
+  fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_nephew_get_answer"));
+  ASSERT_TRUE(fn != nullptr) << dlerror();
+  ASSERT_EQ(42, fn());
+
+  ASSERT_EQ(0, dlclose(handle));
+}
+
+extern "C" int check_order_reloc_root_get_answer_impl() {
+  return 42;
+}
+
+TEST(dlfcn, dlopen_check_order_reloc_main_executable) {
+  // This is how this one works:
+  // we lookup and call get_answer3 which is defined in 'root.so'
+  // and in turn calls external root_get_answer_impl() defined in _2.so and
+  // above the correct _impl() is one in the executable.
+  //
+  // libtest_check_order_reloc_root.so
+  // |
+  // +-> ..._1.so <- empty
+  // |
+  // +-> ..._2.so <- gives incorrect answer for answer_main_impl()
+  //
+
+  void* handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_NOLOAD);
+  ASSERT_TRUE(handle == nullptr);
+#ifdef __BIONIC__
+  // TODO: glibc returns nullptr on dlerror() here. Is it bug?
+  ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_root.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
+#endif
+
+  handle = dlopen("libtest_check_order_reloc_root.so", RTLD_NOW | RTLD_LOCAL);
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+
+  typedef int (*fn_t) (void);
+  fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_root_get_answer"));
+  ASSERT_TRUE(fn != nullptr) << dlerror();
+  ASSERT_EQ(42, fn());
+
+  ASSERT_EQ(0, dlclose(handle));
+}
+
 TEST(dlfcn, dlopen_check_rtld_local) {
   void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
   ASSERT_TRUE(sym == nullptr);
@@ -342,7 +499,6 @@
   ASSERT_TRUE(!is_unloaded);
 }
 
-
 TEST(dlfcn, dlopen_failure) {
   void* self = dlopen("/does/not/exist", RTLD_NOW);
   ASSERT_TRUE(self == NULL);
diff --git a/tests/libs/Android.build.dlopen_check_order_dlsym.mk b/tests/libs/Android.build.dlopen_check_order_dlsym.mk
new file mode 100644
index 0000000..73d8c1a
--- /dev/null
+++ b/tests/libs/Android.build.dlopen_check_order_dlsym.mk
@@ -0,0 +1,90 @@
+#
+# Copyright (C) 2012 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.
+#
+
+# -----------------------------------------------------------------------------
+# Libraries used by dlfcn tests to verify correct load order:
+# libtest_check_order_2_right.so
+# -----------------------------------------------------------------------------
+libtest_check_order_dlsym_2_right_src_files := \
+    dlopen_check_order_dlsym_answer.cpp
+
+libtest_check_order_dlsym_2_right_cflags := -D__ANSWER=42
+module := libtest_check_order_dlsym_2_right
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# libtest_check_order_a.so
+# -----------------------------------------------------------------------------
+libtest_check_order_dlsym_a_src_files := \
+    dlopen_check_order_dlsym_answer.cpp
+
+libtest_check_order_dlsym_a_cflags := -D__ANSWER=1
+module := libtest_check_order_dlsym_a
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# libtest_check_order_b.so
+# -----------------------------------------------------------------------------
+libtest_check_order_dlsym_b_src_files := \
+    dlopen_check_order_dlsym_answer.cpp
+
+libtest_check_order_dlsym_b_cflags := -D__ANSWER=2 -D__ANSWER2=43
+module := libtest_check_order_dlsym_b
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# libtest_check_order_c.so
+# -----------------------------------------------------------------------------
+libtest_check_order_dlsym_3_c_src_files := \
+    dlopen_check_order_dlsym_answer.cpp
+
+libtest_check_order_dlsym_3_c_cflags := -D__ANSWER=3
+module := libtest_check_order_dlsym_3_c
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# libtest_check_order_d.so
+# -----------------------------------------------------------------------------
+libtest_check_order_dlsym_d_src_files := \
+   dlopen_check_order_dlsym_answer.cpp
+
+libtest_check_order_dlsym_d_shared_libraries := libtest_check_order_dlsym_b
+libtest_check_order_dlsym_d_cflags := -D__ANSWER=4 -D__ANSWER2=4
+module := libtest_check_order_dlsym_d
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# libtest_check_order_left.so
+# -----------------------------------------------------------------------------
+libtest_check_order_dlsym_1_left_src_files := \
+    empty.cpp
+
+libtest_check_order_dlsym_1_left_shared_libraries := libtest_check_order_dlsym_a libtest_check_order_dlsym_b
+
+module := libtest_check_order_dlsym_1_left
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# libtest_check_order.so
+# -----------------------------------------------------------------------------
+libtest_check_order_dlsym_src_files := \
+    empty.cpp
+
+libtest_check_order_dlsym_shared_libraries := libtest_check_order_dlsym_1_left \
+  libtest_check_order_dlsym_2_right libtest_check_order_dlsym_3_c
+
+module := libtest_check_order_dlsym
+include $(LOCAL_PATH)/Android.build.testlib.mk
diff --git a/tests/libs/Android.build.dlopen_check_order_reloc_main_executable.mk b/tests/libs/Android.build.dlopen_check_order_reloc_main_executable.mk
new file mode 100644
index 0000000..639696b
--- /dev/null
+++ b/tests/libs/Android.build.dlopen_check_order_reloc_main_executable.mk
@@ -0,0 +1,56 @@
+#
+# Copyright (C) 2012 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.
+#
+
+# -----------------------------------------------------------------------------
+# Libraries used by dlfcn tests to verify correct relocation order:
+# libtest_check_order_reloc_root*.so
+# -----------------------------------------------------------------------------
+
+
+# -----------------------------------------------------------------------------
+# ..._1.so - empty
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_root_1_src_files := \
+    empty.cpp
+
+
+module := libtest_check_order_reloc_root_1
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+
+# -----------------------------------------------------------------------------
+# ..._2.so - this one has the incorrect answer
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_root_2_src_files := \
+    dlopen_check_order_reloc_root_answer_impl.cpp
+
+libtest_check_order_reloc_root_2_cflags := -D__ANSWER=2
+
+module := libtest_check_order_reloc_root_2
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# libtest_check_order_reloc_root.so <- implements get_answer3()
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_root_src_files := \
+    dlopen_check_order_reloc_root_answer.cpp
+
+libtest_check_order_reloc_root_shared_libraries := \
+    libtest_check_order_reloc_root_1 \
+    libtest_check_order_reloc_root_2
+
+module := libtest_check_order_reloc_root
+include $(LOCAL_PATH)/Android.build.testlib.mk
diff --git a/tests/libs/Android.build.dlopen_check_order_reloc_siblings.mk b/tests/libs/Android.build.dlopen_check_order_reloc_siblings.mk
new file mode 100644
index 0000000..0f1a2b4
--- /dev/null
+++ b/tests/libs/Android.build.dlopen_check_order_reloc_siblings.mk
@@ -0,0 +1,133 @@
+#
+# 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.
+#
+
+# -----------------------------------------------------------------------------
+# Libraries used by dlfcn tests to verify correct relocation order:
+# libtest_check_order_reloc_siblings*.so
+# -----------------------------------------------------------------------------
+
+# -----------------------------------------------------------------------------
+# ..._1.so - empty
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_1_src_files := \
+    empty.cpp
+
+libtest_check_order_reloc_siblings_1_shared_libraries := \
+    libtest_check_order_reloc_siblings_a \
+    libtest_check_order_reloc_siblings_b
+
+module := libtest_check_order_reloc_siblings_1
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+
+# -----------------------------------------------------------------------------
+# ..._2.so - empty
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_2_src_files := \
+    empty.cpp
+
+libtest_check_order_reloc_siblings_2_shared_libraries := \
+    libtest_check_order_reloc_siblings_c \
+    libtest_check_order_reloc_siblings_d
+
+module := libtest_check_order_reloc_siblings_2
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# ..._3.so - get_answer2();
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_3_src_files := \
+    dlopen_check_order_reloc_nephew_answer.cpp
+
+libtest_check_order_reloc_siblings_3_shared_libraries := \
+    libtest_check_order_reloc_siblings_e \
+    libtest_check_order_reloc_siblings_f
+
+module := libtest_check_order_reloc_siblings_3
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# ..._a.so <- correct impl
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_a_src_files := \
+    dlopen_check_order_reloc_answer_impl.cpp
+
+libtest_check_order_reloc_siblings_a_cflags := -D__ANSWER=42
+module := libtest_check_order_reloc_siblings_a
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# ..._b.so
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_b_src_files := \
+    dlopen_check_order_reloc_answer_impl.cpp
+
+libtest_check_order_reloc_siblings_b_cflags := -D__ANSWER=1
+module := libtest_check_order_reloc_siblings_b
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# ..._c.so
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_c_src_files := \
+    dlopen_check_order_reloc_answer_impl.cpp
+
+libtest_check_order_reloc_siblings_c_cflags := -D__ANSWER=2
+module := libtest_check_order_reloc_siblings_c
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# ..._d.so
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_d_src_files := \
+    dlopen_check_order_reloc_answer_impl.cpp
+
+libtest_check_order_reloc_siblings_d_cflags := -D__ANSWER=3
+module := libtest_check_order_reloc_siblings_d
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# ..._e.so
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_e_src_files := \
+    dlopen_check_order_reloc_answer_impl.cpp
+
+libtest_check_order_reloc_siblings_e_cflags := -D__ANSWER=4
+module := libtest_check_order_reloc_siblings_e
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# ..._f.so <- get_answer()
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_f_src_files := \
+    dlopen_check_order_reloc_answer.cpp
+
+module := libtest_check_order_reloc_siblings_f
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# libtest_check_order_reloc_siblings.so
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_src_files := \
+    empty.cpp
+
+libtest_check_order_reloc_siblings_shared_libraries := \
+    libtest_check_order_reloc_siblings_1 \
+    libtest_check_order_reloc_siblings_2 \
+    libtest_check_order_reloc_siblings_3
+
+module := libtest_check_order_reloc_siblings
+include $(LOCAL_PATH)/Android.build.testlib.mk
diff --git a/tests/libs/Android.mk b/tests/libs/Android.mk
index 175a635..05e7113 100644
--- a/tests/libs/Android.mk
+++ b/tests/libs/Android.mk
@@ -21,6 +21,9 @@
 common_additional_dependencies := \
     $(LOCAL_PATH)/Android.mk \
     $(LOCAL_PATH)/Android.build.dlext_testzip.mk \
+    $(LOCAL_PATH)/Android.build.dlopen_check_order_dlsym.mk \
+    $(LOCAL_PATH)/Android.build.dlopen_check_order_reloc_siblings.mk \
+    $(LOCAL_PATH)/Android.build.dlopen_check_order_reloc_main_executable.mk \
     $(LOCAL_PATH)/Android.build.testlib.mk \
     $(TEST_PATH)/Android.build.mk
 
@@ -150,79 +153,19 @@
 include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
-# Libraries used by dlfcn tests to verify correct load order:
-# libtest_check_order_2_right.so
+# Build libtest_check_order_dlsym.so with its dependencies.
 # -----------------------------------------------------------------------------
-libtest_check_order_2_right_src_files := \
-    dlopen_testlib_answer.cpp
-
-libtest_check_order_2_right_cflags := -D__ANSWER=42
-module := libtest_check_order_2_right
-include $(LOCAL_PATH)/Android.build.testlib.mk
+include $(LOCAL_PATH)/Android.build.dlopen_check_order_dlsym.mk
 
 # -----------------------------------------------------------------------------
-# libtest_check_order_a.so
+# Build libtest_check_order_siblings.so with its dependencies.
 # -----------------------------------------------------------------------------
-libtest_check_order_a_src_files := \
-    dlopen_testlib_answer.cpp
-
-libtest_check_order_a_cflags := -D__ANSWER=1
-module := libtest_check_order_a
-include $(LOCAL_PATH)/Android.build.testlib.mk
+include $(LOCAL_PATH)/Android.build.dlopen_check_order_reloc_siblings.mk
 
 # -----------------------------------------------------------------------------
-# libtest_check_order_b.so
+# Build libtest_check_order_root.so with its dependencies.
 # -----------------------------------------------------------------------------
-libtest_check_order_b_src_files := \
-    dlopen_testlib_answer.cpp
-
-libtest_check_order_b_cflags := -D__ANSWER=2 -D__ANSWER2=43
-module := libtest_check_order_b
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# libtest_check_order_c.so
-# -----------------------------------------------------------------------------
-libtest_check_order_3_c_src_files := \
-    dlopen_testlib_answer.cpp
-
-libtest_check_order_3_c_cflags := -D__ANSWER=3
-module := libtest_check_order_3_c
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# libtest_check_order_d.so
-# -----------------------------------------------------------------------------
-libtest_check_order_d_src_files := \
-   dlopen_testlib_answer.cpp
-
-libtest_check_order_d_shared_libraries := libtest_check_order_b
-libtest_check_order_d_cflags := -D__ANSWER=4 -D__ANSWER2=4
-module := libtest_check_order_d
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# libtest_check_order_left.so
-# -----------------------------------------------------------------------------
-libtest_check_order_1_left_src_files := \
-    empty.cpp
-
-libtest_check_order_1_left_shared_libraries := libtest_check_order_a libtest_check_order_b
-
-module := libtest_check_order_1_left
-include $(LOCAL_PATH)/Android.build.testlib.mk
-
-# -----------------------------------------------------------------------------
-# libtest_check_order.so
-# -----------------------------------------------------------------------------
-libtest_check_order_src_files := \
-    empty.cpp
-
-libtest_check_order_shared_libraries := libtest_check_order_1_left \
-  libtest_check_order_2_right libtest_check_order_3_c
-
-module := libtest_check_order
-include $(LOCAL_PATH)/Android.build.testlib.mk
+include $(LOCAL_PATH)/Android.build.dlopen_check_order_reloc_main_executable.mk
 
 # -----------------------------------------------------------------------------
 # Library with dependency loop used by dlfcn tests
diff --git a/tests/libs/dlopen_testlib_answer.cpp b/tests/libs/dlopen_check_order_dlsym_answer.cpp
similarity index 87%
rename from tests/libs/dlopen_testlib_answer.cpp
rename to tests/libs/dlopen_check_order_dlsym_answer.cpp
index a4d7504..2ae6cf7 100644
--- a/tests/libs/dlopen_testlib_answer.cpp
+++ b/tests/libs/dlopen_check_order_dlsym_answer.cpp
@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-extern "C" int dlopen_test_get_answer() {
+extern "C" int check_order_dlsym_get_answer() {
   return __ANSWER;
 }
 
 #ifdef __ANSWER2
-extern "C" int dlopen_test_get_answer2() {
+extern "C" int check_order_dlsym_get_answer2() {
   return __ANSWER2;
 }
 #endif
diff --git a/tests/libs/dlopen_testlib_answer.cpp b/tests/libs/dlopen_check_order_reloc_answer.cpp
similarity index 77%
copy from tests/libs/dlopen_testlib_answer.cpp
copy to tests/libs/dlopen_check_order_reloc_answer.cpp
index a4d7504..036670b 100644
--- a/tests/libs/dlopen_testlib_answer.cpp
+++ b/tests/libs/dlopen_check_order_reloc_answer.cpp
@@ -14,12 +14,10 @@
  * limitations under the License.
  */
 
-extern "C" int dlopen_test_get_answer() {
-  return __ANSWER;
+extern "C" int __attribute__((weak)) check_order_reloc_get_answer_impl() {
+  return 0;
 }
 
-#ifdef __ANSWER2
-extern "C" int dlopen_test_get_answer2() {
-  return __ANSWER2;
+extern "C" int check_order_reloc_get_answer() {
+  return check_order_reloc_get_answer_impl();
 }
-#endif
diff --git a/tests/libs/dlopen_testlib_answer.cpp b/tests/libs/dlopen_check_order_reloc_answer_impl.cpp
similarity index 82%
copy from tests/libs/dlopen_testlib_answer.cpp
copy to tests/libs/dlopen_check_order_reloc_answer_impl.cpp
index a4d7504..324b905 100644
--- a/tests/libs/dlopen_testlib_answer.cpp
+++ b/tests/libs/dlopen_check_order_reloc_answer_impl.cpp
@@ -14,12 +14,6 @@
  * limitations under the License.
  */
 
-extern "C" int dlopen_test_get_answer() {
+extern "C" int check_order_reloc_get_answer_impl() {
   return __ANSWER;
 }
-
-#ifdef __ANSWER2
-extern "C" int dlopen_test_get_answer2() {
-  return __ANSWER2;
-}
-#endif
diff --git a/tests/libs/dlopen_testlib_answer.cpp b/tests/libs/dlopen_check_order_reloc_nephew_answer.cpp
similarity index 80%
copy from tests/libs/dlopen_testlib_answer.cpp
copy to tests/libs/dlopen_check_order_reloc_nephew_answer.cpp
index a4d7504..065d1be 100644
--- a/tests/libs/dlopen_testlib_answer.cpp
+++ b/tests/libs/dlopen_check_order_reloc_nephew_answer.cpp
@@ -14,12 +14,8 @@
  * limitations under the License.
  */
 
-extern "C" int dlopen_test_get_answer() {
-  return __ANSWER;
-}
+extern "C" int check_order_reloc_get_answer_impl();
 
-#ifdef __ANSWER2
-extern "C" int dlopen_test_get_answer2() {
-  return __ANSWER2;
+extern "C" int check_order_reloc_nephew_get_answer() {
+  return check_order_reloc_get_answer_impl();
 }
-#endif
diff --git a/tests/libs/dlopen_testlib_answer.cpp b/tests/libs/dlopen_check_order_reloc_root_answer.cpp
similarity index 79%
copy from tests/libs/dlopen_testlib_answer.cpp
copy to tests/libs/dlopen_check_order_reloc_root_answer.cpp
index a4d7504..b21abd7 100644
--- a/tests/libs/dlopen_testlib_answer.cpp
+++ b/tests/libs/dlopen_check_order_reloc_root_answer.cpp
@@ -14,12 +14,8 @@
  * limitations under the License.
  */
 
-extern "C" int dlopen_test_get_answer() {
-  return __ANSWER;
-}
+extern "C" int check_order_reloc_root_get_answer_impl();
 
-#ifdef __ANSWER2
-extern "C" int dlopen_test_get_answer2() {
-  return __ANSWER2;
+extern "C" int check_order_reloc_root_get_answer() {
+  return check_order_reloc_root_get_answer_impl();
 }
-#endif
diff --git a/tests/libs/dlopen_testlib_answer.cpp b/tests/libs/dlopen_check_order_reloc_root_answer_impl.cpp
similarity index 82%
copy from tests/libs/dlopen_testlib_answer.cpp
copy to tests/libs/dlopen_check_order_reloc_root_answer_impl.cpp
index a4d7504..25fb9ac 100644
--- a/tests/libs/dlopen_testlib_answer.cpp
+++ b/tests/libs/dlopen_check_order_reloc_root_answer_impl.cpp
@@ -14,12 +14,6 @@
  * limitations under the License.
  */
 
-extern "C" int dlopen_test_get_answer() {
+extern "C" int check_order_reloc_root_get_answer_impl() {
   return __ANSWER;
 }
-
-#ifdef __ANSWER2
-extern "C" int dlopen_test_get_answer2() {
-  return __ANSWER2;
-}
-#endif