gpt4 book ai didi

linux - LD_PRELOAD 加载事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:59:03 26 4
gpt4 key购买 nike

我写了我的 LD_PRELOAD 模块,我想在我的覆盖函数工作之前添加一些初始化代码。也许 LD_PRELOAD 有任何加载事件或类似的东西?

谢谢!

最佳答案

我不确定“已加载”事件,但是如果您使用的是 gcc,您可能会发现 constructor 属性很有用。举个例子:

测试库.c: #包括

void testing(void) __attribute__((constructor));

void testing(void)
{
printf("It worked!\n");
}

hworld.c:

#include <stdio.h>

int main(void)
{
printf("Hello world!\n");
return 0;
}

$ gcc -o hworld hworld.c
$ gcc -shared -fPIC -o testlib.so testlib.c
$ export LD_PRELOAD=./testlib.so
$ ./hworld
It worked!
Hello world!

constructor属性意味着该函数应该在 main() 之前执行。或者,如果您使用的是 C++,则可以创建类的静态全局实例,其构造函数执行初始化,这将实现与使用 constructor 相同的效果。

关于linux - LD_PRELOAD 加载事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10418805/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com