C++ 类型‘;的强制转换无效;无效*’;函数指针
我想使用C++ 类型‘;的强制转换无效;无效*’;函数指针,c++,C++,我想使用LD_PRELOAD覆盖libc中的open: #include <dlfcn.h> #include <sys/stat.h> extern "C" { int open(const char *path, int flags, mode_t mode) { int (*originalOpen)(const char *path, int flags, mode_t mode); originalOpen = reinterpret_cas
LD_PRELOAD
覆盖libc
中的open
:
#include <dlfcn.h>
#include <sys/stat.h>
extern "C" {
int open(const char *path, int flags, mode_t mode)
{
int (*originalOpen)(const char *path, int flags, mode_t mode);
originalOpen = reinterpret_cast<decltype(originalOpen)>(dlsym(RTLD_NEXT, "open"));
//originalOpen = reinterpret_cast<decltype(open)>(dlsym(RTLD_NEXT, "open"));
//...
return (*originalOpen)(path, flags, mode);
}
}
当我用注释掉的行初始化原始打开时
我使用了gcc 8.0.1版尝试以下代码:
originalOpen = reinterpret_cast<decltype(open) *>(dlsym(RTLD_NEXT, "open"));
originalOpen=reinterpret_cast(dlsym(RTLD_NEXT,“open”);
Decltype获取要创建函数指针时函数的类型。有从函数到其类型的指针的隐式转换,但它们不是等价的。这就是为什么带有decltype(original\u open)
的版本工作得很好-original\u open
的类型是函数指针,而不是函数
originalOpen = reinterpret_cast<decltype(open) *>(dlsym(RTLD_NEXT, "open"));