summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile29
-rw-r--r--libdynsymlink.c23
-rw-r--r--test1.c13
-rw-r--r--test2.c13
4 files changed, 78 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..433667c
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,29 @@
+all: libdynsymlink.so
+PHONY: all
+
+libdynsymlink.so: libdynsymlink.o
+ clang -o libdynsymlink.so --shared libdynsymlink.o -ldl
+
+test1: test1.o libdynsymlink.so
+ clang -o test1 test1.o libdynsymlink.so
+
+test2: test2.o
+ clang -o test2 test2.o
+
+libdynsymlink.o: libdynsymlink.c
+ clang -o libdynsymlink.o -fPIC -c libdynsymlink.c
+
+test1.o: test1.c
+ clang -o test1.o -c test1.c
+
+test2.o: test2.c
+ clang -o test2.o -c test2.c
+
+check: test1 test2 libdynsymlink.so
+ LD_LIBRARY_PATH=. ./test1
+ LD_PRELOAD=./libdynsymlink.so ./test2
+PHONY: check
+
+clean:
+ +rm -f test1 test2 test1.o test2.o libdynsymlink.so libdynsymlink.o
+PHONY: clean
diff --git a/libdynsymlink.c b/libdynsymlink.c
new file mode 100644
index 0000000..f1c225c
--- /dev/null
+++ b/libdynsymlink.c
@@ -0,0 +1,23 @@
+#define _GNU_SOURCE
+#include <dlfcn.h>
+
+#include <stdio.h>
+
+static int dynsym_initialized = 0;
+static int (*real_printf) (const char *);
+
+void dynsym_init(void)
+{
+ if(!dynsym_initialized) {
+ real_printf = dlsym(RTLD_NEXT, "printf");
+ dynsym_initialized = 1;
+ }
+}
+
+int printf(const char *__restrict __format, ...)
+{
+ dynsym_init();
+ (*real_printf)("This is libdynsymlink printf\n");
+ return (*real_printf)(__format);
+}
+
diff --git a/test1.c b/test1.c
new file mode 100644
index 0000000..0feef57
--- /dev/null
+++ b/test1.c
@@ -0,0 +1,13 @@
+/*
+ * test1
+ *
+ * Linked against libdynsymlink
+ */
+
+#include <stdio.h>
+
+int main()
+{
+ printf("This is test1\n");
+ return 0;
+}
diff --git a/test2.c b/test2.c
new file mode 100644
index 0000000..8d990ae
--- /dev/null
+++ b/test2.c
@@ -0,0 +1,13 @@
+/*
+ * test2
+ *
+ * Non-cooperative, run with LD_PRELOAD=libdynsymlink.so
+ */
+
+#include <stdio.h>
+
+int main()
+{
+ printf("This is test2\n");
+ return 0;
+}