Compile time and link time Interpositioning
Deepak Jain
Innovative Technologist | Versatile Architect | Expert in Software Design & Development | Proficient in Problem Solving, Design Patterns, DSA | Skilled in Rust, C/C++ 11/17/20, Multithreading, Socket Programming, STL
Now let's discuss about Run-Time?#Interpositioning:
Compile-time interpositioning requires access to a program’s source files. Link-time interpositioning requires access to its relocatable object files.?#linux?#references?#environment
However, there is a mechanism for interpositioning at run time that requires access only to the executable object file. This fascinating mechanism is based on the dynamic linker’s LD_PRELOAD environment variable.
If the LD_PRELOAD environment variable is set to a list of shared library pathnames (separated by spaces or colons), then when we load and execute a program, the dynamic linker (ld-linux.so) will search the LD_PRELOAD libraries first, before any other shared libraries, when it resolves undefined references. With this mechanism, we can interpose on any function in any shared library, including?libc.so, when we load and execute any executable.
Here is how to build the shared library that contains the wrapper functions:
linux> gcc -DRUNTIME -shared -fpic -o?mymalloc.so?mymalloc.c -ldl
Here is how to compile the main program:
linux> gcc -o intr int.c
Here is how to run the program from the bash shell:
linux> LD_PRELOAD="./mymalloc.so" ./intr