说我有课
class A
{
double f(int x);
};
我想获得
void*
段中表示成员函数 __TEXT
实现的字节的 A::f
。我不想使用指针作为函数指针来调用函数,我只想检查表示机器代码的字节。有没有好的方法可以做到这一点?理想情况下无需使用任何运行时调用。
我正在使用 clang 和 LLVM。
这是我的汇编器想法的“概念证明”类型......
#include <iostream>
class Foo
{
public:
double bar()
{
return 123;
}
};
asm("\t.global test\n"
"\t.data\n"
"\t.align 2\n"
"\t.type test, %object\n"
"\t.size test, 4\n"
"test:\n"
"\t.word _ZN3Foo3barEv\n"
"\t.text\n");
extern void* test;
int main()
{
std::cout << Foo{}.bar() << '\n';
std::cout << test << '\n';
std::cout << ((double(*)())test)() << '\n';
return 0;
}
注意:这是 x64 g++,不是 clang:
stieber@gatekeeper:~ $ g++ Test.cpp && ./a.out
123
0x107e0
123
我相当肯定指针确实指向代码,因为通过疯狂的转换,我什至可以调用它来获得正确的结果......
当我添加
virtual
时,它仍然有效。
当然,非常不便携。