Add dlfcn_test to glibc test suite.

Bug: 18186310

(cherry picked from commit eb27bbae8f0edc6b62ca2db73256c7fb53b9e9bf)

Change-Id: I1d608dfa12dbafbdcdb8bc6d818c5872404c19e0
diff --git a/tests/Android.mk b/tests/Android.mk
index 407f21b..f20eb7d 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -283,6 +283,7 @@
 
 bionic-unit-tests-glibc_src_files := \
     atexit_test.cpp \
+    dlfcn_test.cpp \
 
 bionic-unit-tests-glibc_whole_static_libraries := \
     libBionicStandardTests \
@@ -290,8 +291,12 @@
 bionic-unit-tests-glibc_ldlibs := \
     -lrt -ldl \
 
+bionic-unit-tests-glibc_c_includes := \
+    bionic/libc \
+
 bionic-unit-tests-glibc_cflags := $(test_cflags)
 bionic-unit-tests-glibc_cppflags := $(test_cppflags)
+bionic-unit-tests-glibc_ldflags := -Wl,--export-dynamic
 
 module := bionic-unit-tests-glibc
 module_tag := optional
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index e7787b4..f1ec0f1 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -188,14 +188,14 @@
   // get_answer2() is defined in (b, d)
   void* sym = dlsym(RTLD_DEFAULT, "dlopen_test_get_answer");
   ASSERT_TRUE(sym == nullptr);
-  void* handle = dlopen("libtest_check_order.so", RTLD_NOW);
+  void* handle = dlopen("libtest_check_order.so", RTLD_NOW | RTLD_GLOBAL);
   ASSERT_TRUE(handle != nullptr);
   typedef int (*fn_t) (void);
   fn_t fn, fn2;
   fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "dlopen_test_get_answer"));
-  ASSERT_TRUE(fn != NULL);
+  ASSERT_TRUE(fn != NULL) << dlerror();
   fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "dlopen_test_get_answer2"));
-  ASSERT_TRUE(fn2 != NULL);
+  ASSERT_TRUE(fn2 != NULL) << dlerror();
 
   ASSERT_EQ(42, fn());
   ASSERT_EQ(43, fn2());
@@ -248,6 +248,7 @@
 // libtest_with_dependency_loop_a.so
 TEST(dlfcn, dlopen_check_loop) {
   void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
+#if defined(__BIONIC__)
   ASSERT_TRUE(handle == nullptr);
   ASSERT_STREQ("dlopen failed: recursive link to \"libtest_with_dependency_loop_a.so\"", dlerror());
   // This symbol should never be exposed
@@ -261,6 +262,10 @@
   handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
   ASSERT_TRUE(handle == nullptr);
   ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
+#else // glibc allows recursive links
+  ASSERT_TRUE(handle != nullptr);
+  dlclose(handle);
+#endif
 }
 
 TEST(dlfcn, dlopen_nodelete) {
diff --git a/tests/libs/Android.build.testlib.mk b/tests/libs/Android.build.testlib.mk
new file mode 100644
index 0000000..5b688e4
--- /dev/null
+++ b/tests/libs/Android.build.testlib.mk
@@ -0,0 +1,22 @@
+#
+# 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_target := SHARED_LIBRARY
+build_type := host
+include $(TEST_PATH)/Android.build.mk
+build_type := target
+include $(TEST_PATH)/Android.build.mk
+
diff --git a/tests/libs/Android.mk b/tests/libs/Android.mk
index b7335d5..8746ff2 100644
--- a/tests/libs/Android.mk
+++ b/tests/libs/Android.mk
@@ -29,9 +29,7 @@
 
 module := no-elf-hash-table-library
 module_tag := optional
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 endif
 
 # -----------------------------------------------------------------------------
@@ -45,15 +43,13 @@
 
 module := libdlext_test
 module_tag := optional
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # create symlink to libdlext_test.so for symlink test
 # -----------------------------------------------------------------------------
 # Use = instead of := to defer the evaluation of $@
-$(LOCAL_INSTALLED_MODULE): PRIVATE_POST_INSTALL_CMD = \
+$(TARGET_OUT)/lib/libdlext_test.so: PRIVATE_POST_INSTALL_CMD = \
     $(hide) cd $(dir $@) && ln -sf $(notdir $@) libdlext_test_v2.so
 
 ifneq ($(TARGET_2ND_ARCH),)
@@ -62,6 +58,13 @@
     $(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 - without GNU RELRO program header
 # -----------------------------------------------------------------------------
@@ -108,9 +111,7 @@
     dlopen_testlib_simple.cpp
 
 module := libtest_simple
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # Library used by dlfcn nodelete tests
@@ -150,9 +151,7 @@
 
 libtest_check_order_2_right_cflags := -D__ANSWER=42
 module := libtest_check_order_2_right
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # libtest_check_order_a.so
@@ -162,9 +161,7 @@
 
 libtest_check_order_a_cflags := -D__ANSWER=1
 module := libtest_check_order_a
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # libtest_check_order_b.so
@@ -174,9 +171,7 @@
 
 libtest_check_order_b_cflags := -D__ANSWER=2 -D__ANSWER2=43
 module := libtest_check_order_b
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # libtest_check_order_c.so
@@ -186,9 +181,7 @@
 
 libtest_check_order_3_c_cflags := -D__ANSWER=3
 module := libtest_check_order_3_c
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # libtest_check_order_d.so
@@ -199,9 +192,7 @@
 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
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # libtest_check_order_left.so
@@ -212,9 +203,7 @@
 libtest_check_order_1_left_shared_libraries := libtest_check_order_a libtest_check_order_b
 
 module := libtest_check_order_1_left
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # libtest_check_order.so
@@ -226,9 +215,7 @@
   libtest_check_order_2_right libtest_check_order_3_c
 
 module := libtest_check_order
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # Library with dependency loop used by dlfcn tests
@@ -241,9 +228,7 @@
     libtest_with_dependency_loop_a
 
 module := libtest_with_dependency_loop
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # libtest_with_dependency_loop_a.so
@@ -254,9 +239,7 @@
     libtest_with_dependency_loop_b_tmp
 
 module := libtest_with_dependency_loop_a
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # libtest_with_dependency_loop_b.so
@@ -267,9 +250,7 @@
 libtest_with_dependency_loop_b_tmp_ldflags := -Wl,-soname=libtest_with_dependency_loop_b.so
 
 module := libtest_with_dependency_loop_b_tmp
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # libtest_with_dependency_loop_b.so
@@ -278,9 +259,7 @@
 libtest_with_dependency_loop_b_shared_libraries := libtest_with_dependency_loop_c
 
 module := libtest_with_dependency_loop_b
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # libtest_with_dependency_loop_c.so
@@ -291,9 +270,7 @@
     libtest_with_dependency_loop_a
 
 module := libtest_with_dependency_loop_c
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # libtest_relo_check_dt_needed_order.so
@@ -308,15 +285,13 @@
 libtest_relo_check_dt_needed_order_src_files := dlopen_testlib_relo_check_dt_needed_order.cpp
 libtest_relo_check_dt_needed_order_1_src_files := dlopen_testlib_relo_check_dt_needed_order_1.cpp
 libtest_relo_check_dt_needed_order_2_src_files := dlopen_testlib_relo_check_dt_needed_order_2.cpp
-build_type := target
-build_target := SHARED_LIBRARY
 
 module := libtest_relo_check_dt_needed_order
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 module := libtest_relo_check_dt_needed_order_1
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 module := libtest_relo_check_dt_needed_order_2
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # Library with dependency used by dlfcn tests
@@ -327,22 +302,30 @@
 libtest_with_dependency_shared_libraries := libdlext_test
 
 module := libtest_with_dependency
-build_type := target
-build_target := SHARED_LIBRARY
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # Library used by ifunc tests
 # -----------------------------------------------------------------------------
+libtest_ifunc_src_files := \
+    dlopen_testlib_ifunc.c
+
+libtest_ifunc_clang_host := false
+module := libtest_ifunc
+build_target := SHARED_LIBRARY
+
+build_type := host
+include $(TEST_PATH)/Android.build.mk
+
 ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),x86 x86_64))
-    libtest_ifunc_src_files := \
-        dlopen_testlib_ifunc.c
+    ifeq ($(TARGET_ARCH),arm64)
+      libtest_ifunc_multilib := 64
+      # TODO: This is a workaround - remove it once gcc
+      # removes its Android ifunc checks
+      libtest_ifunc_cflags := -mglibc
+    endif
 
-    LOCAL_SDK_VERSION := current
-    module := libtest_ifunc
     build_type := target
-    build_target := SHARED_LIBRARY
-
     include $(TEST_PATH)/Android.build.mk
 endif
 
@@ -354,11 +337,7 @@
     atexit_testlib.cpp
 
 module := libtest_atexit
-build_target := SHARED_LIBRARY
-build_type := target
-include $(TEST_PATH)/Android.build.mk
-build_type := host
-include $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
 # Library with weak function
@@ -367,14 +346,4 @@
     dlsym_weak_function.cpp
 
 module := libtest_dlsym_weak_func
-build_target := SHARED_LIBRARY
-build_type := target
-include $(TEST_PATH)/Android.build.mk
-build_type := host
-include $(TEST_PATH)/Android.build.mk
-
-LOCAL_ADDITIONAL_DEPENDENCIES := \
-    $(LOCAL_PATH)/Android.mk \
-    $(LOCAL_PATH)/Android.build.dlext_testzip.mk \
-    $(LOCAL_PATH)/Android.build.testlib.mk \
-    $(TEST_PATH)/Android.build.mk
+include $(LOCAL_PATH)/Android.build.testlib.mk
diff --git a/tests/libs/dlopen_testlib_simple.cpp b/tests/libs/dlopen_testlib_simple.cpp
index 06253e1..3226955 100644
--- a/tests/libs/dlopen_testlib_simple.cpp
+++ b/tests/libs/dlopen_testlib_simple.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <stdint.h>
 #include <stdlib.h>
 
 uint32_t dlopen_testlib_taxicab_number = 1729;