2017-08-29 74 views
1
class Base { 
public: 
    Base() {} 
    virtual void Get() { } 
}; 

class Derivered : public Base { 
public: 
    virtual void Get() { } 
}; 

int main() { 
    Base* base = new Derivered(); 
    base->Get(); 
    return 0; 
} 

我使用gcc 5.4.0编译代码,并使用objdump -S a.out反汇编二进制文件。我想查找Base的vptr,但只显示未知地址0x80487d4。最大地址号码是0x80487b7,我无法理解。 命令列表:g++ test.cpp -O0; objdump -S a.out如何在C++汇编代码中找到VPTR?

080486fe <_ZN4BaseC1Ev>: 
80486fe: 55      push %ebp 
80486ff: 89 e5     mov %esp,%ebp 
8048701: ba d4 87 04 08   mov $0x80487d4,%edx 
8048706: 8b 45 08    mov 0x8(%ebp),%eax 
8048709: 89 10     mov %edx,(%eax) 
+3

确保优化关闭。 –

+0

你读过[这篇文章](http://phrack.org/issues/56/8.html)吗? – perror

+1

如果你打开优化,编译器就很容易* devmoreize *函数调用,内联它,甚至消除它,就像在这种情况下, – WhiZTiM

回答

0
080486fe <_ZN4BaseC1Ev>: 
    80486fe: 55      push %ebp 
    80486ff: 89 e5     mov %esp,%ebp 
    8048701: ba d4 87 04 08   mov $0x80487d4,%edx 
    8048706: 8b 45 08    mov 0x8(%ebp),%eax 
    8048709: 89 10     mov %edx,(%eax) 

是...

push %ebp    ;- save frame pointer 
mov %esp, %ebp  ;- mov esp-> ebp -ebp is frame pointer 
mov $0x80487d4, %edx ; load vptr address into edx 
mov 0x8(%ebp), %eax ; ld eax with address of this 
mov %edx,(%eax)  ; store vptr in this byte 0