ldso: dlopen test
authornsz <nsz@port70.net>
Thu, 11 Oct 2012 19:48:20 +0000 (21:48 +0200)
committernsz <nsz@port70.net>
Thu, 11 Oct 2012 19:48:20 +0000 (21:48 +0200)
src/ldso/Makefile [new file with mode: 0644]
src/ldso/dlopen.c [new file with mode: 0644]
src/ldso/dlopen_dso.c [new file with mode: 0644]
src/ldso/test.h [new file with mode: 0644]

diff --git a/src/ldso/Makefile b/src/ldso/Makefile
new file mode 100644 (file)
index 0000000..ed7424c
--- /dev/null
@@ -0,0 +1,3 @@
+include ../../Makefile.inc
+LDFLAGS+=-ldl
+
diff --git a/src/ldso/dlopen.c b/src/ldso/dlopen.c
new file mode 100644 (file)
index 0000000..d3c46f0
--- /dev/null
@@ -0,0 +1,39 @@
+#include <dlfcn.h>
+#include "test.h"
+
+int main()
+{
+       void *h, *g;
+       int *i, *i2;
+       char *s;
+       void (*f)(void);
+
+       h = dlopen("./dlopen_dso.so", RTLD_LAZY|RTLD_LOCAL);
+       if (!h)
+               error("dlopen ./dlopen_dso.so failed: %s\n", dlerror());
+       i = dlsym(h, "i");
+       if (!i)
+               error("dlsym i failed: %s\n", dlerror());
+       if (*i != 1)
+               error("initialization failed: want i=1 got i=%d\n", *i);
+       f = (void (*)(void))dlsym(h, "f");
+       if (!f)
+               error("dlsym f failed: %s\n", dlerror());
+       f();
+       if (*i != 2)
+               error("f call failed: want i=2 got i=%d\n", *i);
+       g = dlopen(0, RTLD_LAZY|RTLD_LOCAL);
+       if (!g)
+               error("dlopen 0 failed: %s\n", dlerror());
+       i2 = dlsym(g, "i");
+       s = dlerror();
+       if (i2 || s == 0)
+               error("dlsym i should have failed\n");
+       h = dlopen("./dlopen_dso.so", RTLD_LAZY|RTLD_GLOBAL);
+       i2 = dlsym(g, "i");
+       if (!i2)
+               error("dlsym i failed: %s\n", dlerror());
+       if (*i2 != 2)
+               error("want i2=2, got i2=%d\n", *i2);
+       return test_status;
+}
diff --git a/src/ldso/dlopen_dso.c b/src/ldso/dlopen_dso.c
new file mode 100644 (file)
index 0000000..cb3220a
--- /dev/null
@@ -0,0 +1,6 @@
+int i = 1;
+
+void f(void)
+{
+       i++;
+}
diff --git a/src/ldso/test.h b/src/ldso/test.h
new file mode 100644 (file)
index 0000000..a6a7706
--- /dev/null
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <stdarg.h>
+#define error(...) test_error(__FILE__, __LINE__, __VA_ARGS__)
+
+static int test_status;
+
+static int test_error(const char *n, int l, const char *s, ...)
+{
+       va_list ap;
+
+       if (test_status == 0)
+               printf("FAIL\n");
+       test_status = 1;
+       printf(" ERROR %s:%d: ", n, l);
+       va_start(ap, s);
+       vprintf(s, ap);
+       va_end(ap);
+       return -1;
+}
+