This was blatantly stolen from technovelty, kept here because I hate it when my bookmarks die.
For some reason, people seem to get this quite wrong a lot of the time. Certainly one should not be playing with symbols that start with __ unless you really know what you’re doing with them.
ianw@lime:~/tmp/override$ cat override.c #define _GNU_SOURCE 1 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <dlfcn.h> pid_t getpid(void) { pid_t (*orig_getpid)(void) = dlsym(RTLD_NEXT, "getpid"); printf("Calling GETPID\n"); return orig_getpid(); } ianw@lime:~/tmp/override$ cat test.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { printf("%d\n", getpid()); } ianw@lime:~/tmp/override$ gcc -shared -fPIC -o liboverride.so override.c -ldl ianw@lime:~/tmp/override$ gcc -o test test.c ianw@lime:~/tmp/override$ LD_PRELOAD=./liboverride.so ./test Calling GETPID 15187
1 Comment
I’ve used a similar and rather ugly trick to run old mysqldump util from mysql 5.0 package on a machine with mysql 5.6 installed:
$ cat /opt/mysql-5.0.95/mysqldump_5.0.95.sh
#!/bin/bash
LD_PRELOAD=/opt/mysql-5.0.95/lib64/mysql/libmysqlclient.so.15 /opt/mysql-5.0.95/bin/mysqldump “$@”