diff options
| author | eroen | 2015-07-10 22:59:24 +0200 |
|---|---|---|
| committer | eroen | 2015-07-10 22:59:24 +0200 |
| commit | 1ff4e0626696cce79347cea1f8af03ff0df5c3fd (patch) | |
| tree | 434448c9801009a8365e67a3130571042468c3f2 | |
| download | dynsymlink-1ff4e0626696cce79347cea1f8af03ff0df5c3fd.tar.gz dynsymlink-1ff4e0626696cce79347cea1f8af03ff0df5c3fd.tar.bz2 dynsymlink-1ff4e0626696cce79347cea1f8af03ff0df5c3fd.tar.xz | |
Simple LD_PRELOAD demo
| -rw-r--r-- | Makefile | 29 | ||||
| -rw-r--r-- | libdynsymlink.c | 23 | ||||
| -rw-r--r-- | test1.c | 13 | ||||
| -rw-r--r-- | test2.c | 13 |
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); +} + @@ -0,0 +1,13 @@ +/* + * test1 + * + * Linked against libdynsymlink + */ + +#include <stdio.h> + +int main() +{ + printf("This is test1\n"); + return 0; +} @@ -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; +} |
