Linux 什么是 LD_PRELOAD 技巧?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/426230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:51:36  来源:igfitidea点击:

What is the LD_PRELOAD trick?

clinuxenvironment-variables

提问by Hank Gay

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

我最近在proggit上遇到了对它的引用,并且(截至目前)没有解释。

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

我怀疑可能是它,但我不确定。

采纳答案by JesperE

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

如果您设置LD_PRELOAD为共享对象的路径,则该文件将任何其他库(包括 C 运行时libc.so之前加载。因此,要运行ls您的特殊malloc()实现,请执行以下操作:

$ LD_PRELOAD=/path/to/my/malloc.so /bin/ls

回答by Joshua

You can override symbols in the stock libraries by creating a library with the same symbols and specifying the library in LD_PRELOAD.

您可以通过创建具有相同符号的库并在LD_PRELOAD.

Some people use it to specify libraries in nonstandard locations, but LD_LIBRARY_PATHis better for that purpose.

有些人用它来指定非标准位置的库,但LD_LIBRARY_PATH更适合这个目的。

回答by Ronny Brendel

With LD_PRELOADyou can give libraries precedence.

有了LD_PRELOAD你可以给图书馆优先。

For example you can write a library which implement mallocand free. And by loading these with LD_PRELOADyour mallocand freewill be executed rather than the standard ones.

例如,您可以编写一个实现malloc和的库free。并通过加载这些与LD_PRELOAD您的mallocfree将被执行而不是标准的。

回答by Rajesh

LD_PRELOADlists shared libraries with functions that override the standard set, just as /etc/ld.so.preloaddoes. 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.

LD_PRELOAD列出具有覆盖标准集的函数的共享库,就像那样/etc/ld.so.preload。这些是由 loader 实现的/lib/ld-linux.so。如果您只想覆盖几个选定的功能,您可以通过创建覆盖目标文件和设置来实现LD_PRELOAD;此目标文件中的函数将只覆盖那些保留其他函数的函数。

For more information on shared libraries visit http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

有关共享库的更多信息,请访问 http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

回答by JulienGenoud

it's easy to export mylib.soto env:

很容易导出mylib.so到 env:

$ export LD_PRELOAD=/path/mylib.so
$ ./mybin

to disable :

禁用:

$ export LD_PRELOAD=

回答by asn

Here is a detailed blog post about preloading:

这是有关预加载的详细博客文章:

https://blog.cryptomilk.org/2014/07/21/what-is-preloading/

https://blog.cryptomilk.org/2014/07/21/what-is-preloading/

回答by dnahc araknayirp

Using LD_PRELOADpath, you can force the application loader to load provided shared object, over the default provided.

使用LD_PRELOAD路径,您可以强制应用程序加载器加载提供的共享对象,而不是默认提供的共享对象。

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.

通过使用准备好的共享对象覆盖现有功能,我们已经使用它来破解某些应用程序。

回答by Patric

As many people mentioned, using LD_PRELOADto preload library. BTW, you can CHECKif the setting is available by lddcommand.

正如很多人提到的,LD_PRELOAD用于预加载库。顺便说一句,您可以通过命令检查该设置是否可用ldd

Example: suppose you need to preload your own libselinux.so.1.

示例:假设您需要预加载自己的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)
    ...

回答by Sumith Senarathne

when LD_PRELOAD is used that file will be loaded before any other $export LD_PRELOAD=/path/liblib to be pre loaded, even this can be used in programs too

当使用 LD_PRELOAD 时,该文件将在任何其他 $export LD_PRELOAD=/path/lib要预加载的库之前加载,即使这也可以在程序中使用