Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the LD_PRELOAD trick?

I came across a reference to it recently on proggit and (as of now) it is not explained.

I suspect this might be it, but I don't know for sure.

like image 448
Hank Gay Avatar asked Jan 08 '09 22:01

Hank Gay


People also ask

How does LD_PRELOAD work?

LD_PRELOAD allows you to override symbols in any library by specifying your new function in a shared object. When you run LD_PRELOAD=/path/to/my/free.so /bin/mybinary , /path/to/my/free.so is loaded before any other library, including libc. When mybinary is executed, it uses your custom function for free .

Is LD_PRELOAD an environment variable?

LD_PRELOAD is an optional environmental variable containing one or more paths to shared libraries, or shared objects, that the loader will load before any other shared library including the C runtime library (libc.so) This is called preloading a library.

What is Ld so preload?

LD_PRELOAD. A list of additional, user-specified, ELF shared libraries to be loaded before all others. The items of the list can be separated by spaces or colons. This can be used to selectively override functions in other shared libraries.


1 Answers

If you set LD_PRELOAD to the path of a shared object, that file will be loaded before any other library (including the C runtime, libc.so). So to run ls with your special malloc() implementation, do this:

$ LD_PRELOAD=/path/to/my/malloc.so /bin/ls 
like image 99
JesperE Avatar answered Sep 23 '22 03:09

JesperE