本文介绍了我可以用错误的签名调用用dlsym()导入的函数,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
host.cpp具有:
host.cpp has:
int main (void)
{
void * th = dlopen("./p1.so", RTLD_LAZY);
void * fu = dlsym(th, "fu");
((void(*)(int, const char*)) fu)(2, "rofl");
return 0;
}
p1.cpp具有:
#include <iostream>
extern "C" bool fu (float * lol)
{
std::cout << "fuuuuuuuu!!!\n";
return true;
}
(我故意留出错误)
执行主机时, fuuuuuuuuu !!!即使我用完全不同的函数签名强制转换了指向符号的void指针,也可以正确打印。
When executing host, "fuuuuuuuu!!!" is printed correctly, even though I typecasted the void pointer to the symbol with a completely different function signature.
为什么会发生这种情况,并且这种行为在不同的编译器之间是一致的? / p>
Why did this happen and is this behavior consistent between different compilers?
推荐答案
因为在void指针中没有有关函数签名的信息。或除地址外的任何信息。如果您开始使用参数,可能会遇到麻烦。
Because there's no information about function signature in void pointer. Or any information besides the address. You might get in trouble if you started to use parameters, tho.
这篇关于我可以用错误的签名调用用dlsym()导入的函数,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!