aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/rtdl/scope1
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mlibc/tests/rtdl/scope1')
-rw-r--r--lib/mlibc/tests/rtdl/scope1/libbar.c14
-rw-r--r--lib/mlibc/tests/rtdl/scope1/libfoo.c9
-rw-r--r--lib/mlibc/tests/rtdl/scope1/meson.build7
-rw-r--r--lib/mlibc/tests/rtdl/scope1/test.c90
4 files changed, 120 insertions, 0 deletions
diff --git a/lib/mlibc/tests/rtdl/scope1/libbar.c b/lib/mlibc/tests/rtdl/scope1/libbar.c
new file mode 100644
index 0000000..ecf043e
--- /dev/null
+++ b/lib/mlibc/tests/rtdl/scope1/libbar.c
@@ -0,0 +1,14 @@
+char *foo(void);
+char *foo_global(void);
+
+char *bar() {
+ return "bar";
+}
+
+char *bar_calls_foo() {
+ return foo();
+}
+
+char *bar_calls_foo_global() {
+ return foo_global();
+}
diff --git a/lib/mlibc/tests/rtdl/scope1/libfoo.c b/lib/mlibc/tests/rtdl/scope1/libfoo.c
new file mode 100644
index 0000000..b4e1b8c
--- /dev/null
+++ b/lib/mlibc/tests/rtdl/scope1/libfoo.c
@@ -0,0 +1,9 @@
+char *foo() {
+ return "foo";
+}
+
+char global[] = "foo global";
+
+char *foo_global() {
+ return global;
+}
diff --git a/lib/mlibc/tests/rtdl/scope1/meson.build b/lib/mlibc/tests/rtdl/scope1/meson.build
new file mode 100644
index 0000000..acb679e
--- /dev/null
+++ b/lib/mlibc/tests/rtdl/scope1/meson.build
@@ -0,0 +1,7 @@
+libfoo = shared_library('foo', 'libfoo.c')
+libbar = shared_library('bar', 'libbar.c', build_rpath: test_rpath, link_with: libfoo)
+test_depends = [libfoo, libbar]
+
+libfoo_native = shared_library('native-foo', 'libfoo.c', native: true)
+libbar_native = shared_library('native-bar', 'libbar.c', build_rpath: test_rpath, link_with: libfoo_native, native: true)
+test_native_depends = [libfoo_native, libbar_native]
diff --git a/lib/mlibc/tests/rtdl/scope1/test.c b/lib/mlibc/tests/rtdl/scope1/test.c
new file mode 100644
index 0000000..c19915d
--- /dev/null
+++ b/lib/mlibc/tests/rtdl/scope1/test.c
@@ -0,0 +1,90 @@
+#include <stddef.h>
+#include <dlfcn.h>
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+
+#ifdef USE_HOST_LIBC
+#define LIBFOO "libnative-foo.so"
+#define LIBBAR "libnative-bar.so"
+#else
+#define LIBFOO "libfoo.so"
+#define LIBBAR "libbar.so"
+#endif
+
+typedef char *strfn(void);
+
+int main() {
+ // We haven't dlopen'd these libs yet, so symbol resolution should fail.
+ assert(dlsym(RTLD_DEFAULT, "foo") == NULL);
+ assert(dlsym(RTLD_DEFAULT, "bar") == NULL);
+
+ assert(!dlopen(LIBFOO, RTLD_NOLOAD));
+ assert(!dlopen(LIBBAR, RTLD_NOLOAD));
+
+ void *foo_handle = dlopen(LIBFOO, RTLD_LOCAL | RTLD_NOW);
+ assert(foo_handle);
+ assert(dlopen(LIBFOO, RTLD_NOLOAD | RTLD_NOW));
+
+ strfn *foo_sym = dlsym(foo_handle, "foo");
+ assert(foo_sym);
+ assert(foo_sym());
+ assert(!strcmp(foo_sym(), "foo"));
+
+ strfn *foo_global_sym = dlsym(foo_handle, "foo_global");
+ assert(foo_global_sym);
+ assert(foo_global_sym());
+ assert(!strcmp(foo_global_sym(), "foo global"));
+
+ assert(dlsym(foo_handle, "doesnotexist") == NULL);
+
+ // Nested opening should work
+ assert(dlopen(LIBFOO, RTLD_LOCAL | RTLD_NOW) == foo_handle);
+ assert(dlopen(LIBFOO, RTLD_NOLOAD | RTLD_NOW));
+
+ // Since we've loaded the same library twice, the addresses should be the same
+ assert(dlsym(foo_handle, "foo") == foo_sym);
+ assert(dlsym(foo_handle, "foo_global") == foo_global_sym);
+
+ // libfoo was opened with RTLD_LOCAL, so we shouldn't be able to lookup
+ // its symbols in the global namespace.
+ assert(dlsym(RTLD_DEFAULT, "foo") == NULL);
+
+ {
+ void *bar_handle = dlopen(LIBBAR, RTLD_GLOBAL | RTLD_NOW);
+ assert(bar_handle);
+ assert(dlopen(LIBBAR, RTLD_NOLOAD | RTLD_NOW));
+
+ strfn *bar_sym = dlsym(bar_handle, "bar");
+ assert(bar_sym);
+ assert(bar_sym());
+ assert(!strcmp(bar_sym(), "bar"));
+
+ strfn *bar_calls_foo_sym = dlsym(bar_handle, "bar_calls_foo");
+ assert(bar_calls_foo_sym);
+ assert(bar_calls_foo_sym());
+ assert(!strcmp(bar_calls_foo_sym(), "foo"));
+
+ strfn *bar_calls_foo_global_sym = dlsym(bar_handle, "bar_calls_foo_global");
+ assert(bar_calls_foo_global_sym);
+ assert(bar_calls_foo_global_sym());
+ assert(!strcmp(bar_calls_foo_global_sym(), "foo global"));
+
+ // libbar was opened with RTLD_GLOBAL, so we can find symbols by
+ // searching in the global scope.
+ strfn *new_bar_sym = dlsym(RTLD_DEFAULT, "bar");
+ assert(new_bar_sym);
+ assert(new_bar_sym == bar_sym);
+
+ // Note that we loaded libbar with RTLD_GLOBAL, which should pull
+ // in libfoo's symbols globally too.
+ strfn *new_foo_sym = dlsym(RTLD_DEFAULT, "foo");
+ assert(new_foo_sym);
+ assert(new_foo_sym == foo_sym);
+
+ assert(dlclose(bar_handle) == 0);
+ }
+
+ assert(dlclose(foo_handle) == 0);
+ assert(dlclose(foo_handle) == 0);
+}