summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid 'Digit' Turner <digit@google.com>2010-07-21 16:15:28 -0700
committerDavid 'Digit' Turner <digit@google.com>2010-07-21 16:15:28 -0700
commit6f09971503f9bea177168d1599e925a5b22d18dd (patch)
tree10467d1a98c179f5e853ac73b913b17547b8dc68
parent83b47ccbc4066ef19f5d95261d8a892567a7f0a7 (diff)
downloadextras-6f09971503f9bea177168d1599e925a5b22d18dd.tar.gz
bionic: add a test for dlopen(NULL,...)
Change-Id: Iee8b33d1a046a71f5fd934912ee36371c1c9e8e5
-rw-r--r--tests/bionic/libc/Android.mk23
-rw-r--r--tests/bionic/libc/common/test_dlopen_null.c37
2 files changed, 59 insertions, 1 deletions
diff --git a/tests/bionic/libc/Android.mk b/tests/bionic/libc/Android.mk
index 48748508..10ed2027 100644
--- a/tests/bionic/libc/Android.mk
+++ b/tests/bionic/libc/Android.mk
@@ -32,10 +32,12 @@ define device-test
$(eval LOCAL_MODULE := $(notdir $(file:%.c=%))) \
$(eval LOCAL_MODULE := $(LOCAL_MODULE:%.cpp=%)) \
$(eval LOCAL_CFLAGS += $(EXTRA_CFLAGS)) \
+ $(eval LOCAL_LDFLAGS += $(EXTRA_LDLIBS)) \
$(eval LOCAL_MODULE_TAGS := tests) \
$(eval include $(BUILD_EXECUTABLE)) \
) \
- $(eval EXTRA_CFLAGS :=)
+ $(eval EXTRA_CFLAGS :=) \
+ $(eval EXTRA_LDLIBS :=)
endef
# same as 'device-test' but builds a host executable instead
@@ -82,6 +84,25 @@ EXTRA_CFLAGS := -D_XOPEN_SOURCE=600 -DHOST
$(call host-test, $(sources))
$(call device-test, $(sources))
+# The 'test_dlopen_null' tests requires specific linker flags
+#
+# The -Wl,--export-dynamic ensures that dynamic symbols are
+# exported from the executable.
+#
+# -Wl,-u,foo is used to ensure that symbol "foo" is not
+# garbage-collected by the gold linker, since the function
+# appears to be unused.
+#
+sources := common/test_dlopen_null.c \
+
+EXTRA_LDLIBS := -ldl -Wl,--export-dynamic -Wl,-u,foo
+EXTRA_CFLAGS := -DHOST
+$(call host-test, $(sources))
+
+EXTRA_LDLIBS := -ldl -Wl,--export-dynamic -Wl,-u,foo
+$(call device-test, $(sources))
+
+
sources := \
common/test_libgen.c \
diff --git a/tests/bionic/libc/common/test_dlopen_null.c b/tests/bionic/libc/common/test_dlopen_null.c
new file mode 100644
index 00000000..42b23dd3
--- /dev/null
+++ b/tests/bionic/libc/common/test_dlopen_null.c
@@ -0,0 +1,37 @@
+#include <dlfcn.h>
+#include <stddef.h>
+#include <stdio.h>
+
+extern int foo(void)
+{
+ return 42;
+}
+
+int (*func_ptr)(void) = foo;
+
+int main(void)
+{
+ void* lib = dlopen(NULL, RTLD_NOW | RTLD_GLOBAL);
+ void* symbol;
+
+#if 0
+ /* The Gold linker will garbage-collect unused global functions
+ * even if --Wl,--export-dynamic is used. So use a dummy global
+ * variable reference here to prevent this.
+ */
+ if (foo() != 42)
+ return 3;
+#endif
+
+ if (lib == NULL) {
+ fprintf(stderr, "Could not open self-executable with dlopen(NULL) !!: %s\n", dlerror());
+ return 1;
+ }
+ symbol = dlsym(lib, "foo");
+ if (symbol == NULL) {
+ fprintf(stderr, "Could not lookup symbol inside executable !!: %s\n", dlerror());
+ return 2;
+ }
+ dlclose(lib);
+ return 0;
+}