A Simple LD_PRELOAD Tutorial, Part Two
本文转载自:
Last time I showed you how to override functions in shared libraries by creating your own shared library and preloading it via the LD_PRELOAD
environment variable. Today I’ll show you how to call the original function from the overridden function.
First, let’s review the code example that we used in the previous article. We had a program called prog.c
that used fopen
:
1 |
|
Today let’s write a shared library called myfopen.c
that overrides fopen
in prog.c
and calls the original fopen
from the C standard library:
1 |
|
This shared library exports the fopen
function that prints the path and then uses dlsym
with the RTLD_NEXT
pseudohandle to find the original fopen
function.
We must define the _GNU_SOURCE feature macro in order to get the RTLD_NEXT
definition from <dlfcn.h>
. What RTLD_NEXT
does is finds the next occurrence of a function in the search order after the current library.
We can compile this shared library this way:
1 | gcc -Wall -fPIC -shared -o myfopen.so myfopen.c -ldl |
When we preload it and run prog
we get the following output:
1 | LD_PRELOAD=./myfopen.so ./prog |
It prints the filename of the file we’re opening and successfully opens it.
This is really useful if you need to change how a part of a program works or do some advanced debugging.