【发布时间】:2014-06-19 19:00:54
【问题描述】:
我正在编写一个共享库到 LD_PRELOAD 并拦截来自现有库(在 linux 中)的一些调用。
我有大约 50 多个不同的函数原型和属性声明要编写,我希望代码尽可能短,因为函数原型非常大。
我遇到的问题如下:假设我想拦截对 doStuff(int, void*) 的所有调用
我有以下代码:
头文件:
typedef int (*doStuffPrototype) (int, void*);
extern doStuffPrototype dlSym_doStuff;
extern int doStuff(int, void*);
C 文件
doStuffPrototype dlSym_doStuff;
__attribute__((constructor)) void libSomething() {
void* lib_ptr;
dlerror();
lib_ptr = dlopen(LIB_NAME, RTLD_LAZY);
...
// Loading all references to the real library
dlSym_doStuff = (doStuffPrototype) dlSym(lib_ptr, "doStuff");
}
好的,现在这工作正常,但我想替换标题中的以下行:
extern int doStuff(int, void*);
类似:
extern doStuffPrototype doStuff;
然后我明白了
'doStuff' 重新声明为不同类型的符号
因为它是在真正的库中声明的......但是......它与当前的语法没有问题......我必须重新编写参数...... 如果我取消对 typedef 的引用:
typedef int (doStuffPrototype) (int, void*);
然后extern doStuffPrototype doStuff; 工作但dlSym_doStuff = (doStuffPrototype) dlSym(lib_ptr, "doStuff"); 不编译...
我尝试了很多东西:这可能吗?
【问题讨论】:
-
对,我更正了,谢谢
-
是崩溃还是无法编译?
-
不编译...我会在问题描述中说明
标签: c header-files dlsym