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.
This question is related to
c
linux
environment-variables
With LD_PRELOAD
you can give libraries precedence.
For example you can write a library which implement malloc
and free
. And by loading these with LD_PRELOAD
your malloc
and free
will be executed rather than the standard ones.
You can override symbols in the stock libraries by creating a library with the same symbols and specifying the library in LD_PRELOAD
.
Some people use it to specify libraries in nonstandard locations, but LD_LIBRARY_PATH
is better for that purpose.
Here is a detailed blog post about preloading:
As many people mentioned, using LD_PRELOAD
to preload library. BTW, you can CHECK if the setting is available by ldd
command.
Example: suppose you need to preload your own libselinux.so.1
.
> ldd /bin/ls
...
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f3927b1d000)
libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f3927914000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f392754f000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f3927311000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f392710c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3927d65000)
libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f3926f07000)
Thus, set your preload environment:
export LD_PRELOAD=/home/patric/libselinux.so.1
Check your library again:
>ldd /bin/ls
...
libselinux.so.1 =>
/home/patric/libselinux.so.1 (0x00007fb9245d8000)
...
Here is a detailed blog post about preloading:
With LD_PRELOAD
you can give libraries precedence.
For example you can write a library which implement malloc
and free
. And by loading these with LD_PRELOAD
your malloc
and free
will be executed rather than the standard ones.
it's easy to export mylib.so
to env:
$ export LD_PRELOAD=/path/mylib.so
$ ./mybin
to disable :
$ export LD_PRELOAD=
You can override symbols in the stock libraries by creating a library with the same symbols and specifying the library in LD_PRELOAD
.
Some people use it to specify libraries in nonstandard locations, but LD_LIBRARY_PATH
is better for that purpose.
With LD_PRELOAD
you can give libraries precedence.
For example you can write a library which implement malloc
and free
. And by loading these with LD_PRELOAD
your malloc
and free
will be executed rather than the standard ones.
LD_PRELOAD
lists shared libraries with functions that override the standard set, just as /etc/ld.so.preload
does. These are implemented by the loader /lib/ld-linux.so
. If you want to override just a few selected functions, you can do this by creating an overriding object file and setting LD_PRELOAD
; the functions in this object file will override just those functions leaving others as they were.
For more information on shared libraries visit http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
when LD_PRELOAD is used that file will be loaded before any other
$export LD_PRELOAD=/path/lib
lib to be pre loaded, even this can be used in programs too
With LD_PRELOAD
you can give libraries precedence.
For example you can write a library which implement malloc
and free
. And by loading these with LD_PRELOAD
your malloc
and free
will be executed rather than the standard ones.
Using LD_PRELOAD
path, you can force the application loader to load provided shared object, over the default provided.
Developers uses this to debug their applications by providing different versions of the shared objects.
We've used it to hack certain applications, by overriding existing functions using prepared shared objects.
As many people mentioned, using LD_PRELOAD
to preload library. BTW, you can CHECK if the setting is available by ldd
command.
Example: suppose you need to preload your own libselinux.so.1
.
> ldd /bin/ls
...
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f3927b1d000)
libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f3927914000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f392754f000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f3927311000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f392710c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3927d65000)
libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f3926f07000)
Thus, set your preload environment:
export LD_PRELOAD=/home/patric/libselinux.so.1
Check your library again:
>ldd /bin/ls
...
libselinux.so.1 =>
/home/patric/libselinux.so.1 (0x00007fb9245d8000)
...
when LD_PRELOAD is used that file will be loaded before any other
$export LD_PRELOAD=/path/lib
lib to be pre loaded, even this can be used in programs too
You can override symbols in the stock libraries by creating a library with the same symbols and specifying the library in LD_PRELOAD
.
Some people use it to specify libraries in nonstandard locations, but LD_LIBRARY_PATH
is better for that purpose.
it's easy to export mylib.so
to env:
$ export LD_PRELOAD=/path/mylib.so
$ ./mybin
to disable :
$ export LD_PRELOAD=
LD_PRELOAD
lists shared libraries with functions that override the standard set, just as /etc/ld.so.preload
does. These are implemented by the loader /lib/ld-linux.so
. If you want to override just a few selected functions, you can do this by creating an overriding object file and setting LD_PRELOAD
; the functions in this object file will override just those functions leaving others as they were.
For more information on shared libraries visit http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
Source: Stackoverflow.com