浏览代码

Add a dlopen test

Eric Andersen 24 年之前
父节点
当前提交
d9ea262db5
共有 3 个文件被更改,包括 66 次插入0 次删除
  1. 20 0
      test/ldso/Makefile
  2. 39 0
      test/ldso/dltest.c
  3. 7 0
      test/ldso/howdy.c

+ 20 - 0
test/ldso/Makefile

@@ -0,0 +1,20 @@
+TESTDIR=../
+include $(TESTDIR)/Rules.mak
+
+all: dltest shared run
+
+dltest:
+	$(TESTCC) $(TEST_CFLAGS) -c dltest.c -o dltest.o
+	$(TESTCC) $(TEST_CFLAGS) -c howdy.c -o howdy.o
+
+shared:
+	$(TESTCC) $(TEST_CFLAGS) -shared -o libhowdy.so -Wl,-soname,libhowdy.so howdy.o
+	$(TESTCC) $(TEST_CFLAGS) -o dltest dltest.o -ldl
+	
+run:
+	@echo Running dltest
+	./dltest
+
+clean:
+	rm -f *.o *.so dltest core
+

+ 39 - 0
test/ldso/dltest.c

@@ -0,0 +1,39 @@
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <dlfcn.h>
+
+extern void _dlinfo();
+
+int main(int argc, char **argv) {
+	void *handle;
+	int (*myhowdy)(const char *s);
+	char *error;
+
+#ifdef __UCLIBC__
+	_dlinfo();   /* not supported by ld.so.2 */
+#endif
+
+	handle = dlopen ("./libhowdy.so", RTLD_LAZY);
+
+	if (!handle) {
+		fputs (dlerror(), stderr);
+		exit(1);
+	}
+
+	myhowdy = dlsym(handle, "howdy");
+	if ((error = dlerror()) != NULL)  {
+		fputs(error, stderr);
+		exit(1);
+	}
+
+	myhowdy("hello world!\n");
+
+#ifdef __UCLIBC__
+	_dlinfo();   /* not supported by ld.so.2 */
+#endif
+
+	dlclose(handle);
+
+	return EXIT_SUCCESS;
+}

+ 7 - 0
test/ldso/howdy.c

@@ -0,0 +1,7 @@
+#include <stdio.h>
+
+int howdy(const char *s)
+{
+	return printf("howdy: %s\n", s);
+}
+