【发布时间】:2023-12-26 20:32:01
【问题描述】:
如下定义共享库:
#include <unistd.h>
#include <stdio.h>
static void init(void) __attribute__((constructor));
static void init(void)
{
fprintf(stderr, "pid=%u\n", (unsigned) getpid());
}
在 AMD64 机器上使用 GCC 10 构建:
gcc -shared -fPIC lib.c
运行一个简单的过程来验证:
$ LD_PRELOAD=`realpath a.out` ls
pid=15771
a.out lib.c
pid=15771 行按预期由init() 打印。
现在,重复一个产生子和线程的复杂过程:
$ LD_PRELOAD=`realpath a.out` python3
pid=15835
pid=15835
pid=15835
pid=15835
pid=15839
pid=15844
pid=15835
pid=15835
pid=15846
pid=15846
pid=15847
pid=15847
pid=15849
pid=15849
pid=15851
pid=15852
pid=15853
pid=15853
pid=15856
pid=15857
pid=15857
pid=15858
pid=15858
pid=15861
pid=15862
pid=15862
pid=15865
pid=15868
pid=15835
Python 3.8.2 (default, Apr 19 2020, 18:33:14)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
观察到有pid=15835这样的重复条目,说明init()在某些进程中被执行了不止一次。
为什么?
【问题讨论】:
标签: linux gcc shared-libraries ld ld-preload