我正在查看来自 IDA pro 的代码转储。有一个功能作为这种布局:
garbled_name(int this...
unsigned int v5 ;
v5 = *(_Byte *)(this + 4);
...
我真正好奇的是“+ 4”到底在做什么?这是添加还是其他?
谢谢
代码采用整数“this”,将其加 4,将其转换为指向字节的指针,然后将“v5”设置为该地址处字节的值。
它只是 C++ 类的成员函数,this
是指向对象的指针。该对象的签名可能是:
class some_class {
int i; // int, void*, short, anything with sizeof() <= 4, and it's not char.
// It also can be absent if it's a virtual class (AFAIK it's compiler dependend)
unsigned char c; // or c[N]
...
};
有问题的代码是:
some_class::some_fn(...){
unsigned int v5 = c; // or c[0]
...
};
它是对对象开头的第五个字节的引用。根据生成该代码的编译器,很可能是对象实例中位于第五个字节的类顺序中的项。
编辑:叹息,我错过了“IDA Pro”部分。我将把它留在这里以供娱乐,以防有人想知道“this+4”在普通 C++ 代码中的作用。
"this+4" 获取当前的 this 指针,向前移动四倍的大小。然后它将它转换为字节指针并读取它。
考虑一下:
struct A {
void foo();
int x;
int y;
};
在 32 位系统上,sizeof(A) 很可能是 8 个字节。
A myArray[8];
A *pA = myArray;
现在 pA 指向 &myArray[0]。
pA++;
现在 pA 指向 &myArray[1],即它向前移动了 8 个字节。
void A::foo() {
A *pA = this + 4;
}
如果您在 &myArray[0] 上调用它,它将指向 &myArray[4],即进一步的 32 个字节。